Postgresql – postgres jsonb return specific paths of json object

jsonpostgresql

Consider the following jsonb object:

{
  "track": {
    "segments": [
      {
        "location":   [ 47.763, 13.4034 ],
        "startTime": "2018-10-14 10:05:14",
        "HR": 73
      },
      {
        "location":   [ 47.706, 13.2635 ],
        "startTime": "2018-10-14 10:39:21",
        "HR": 135
      }
    ]
  }
}

I would like to query the object, to return only the following jsonb object, given the folloiwng paths:

  • track.segments.hr
{
  "track": {
    "segments": [
      {
        "HR": 73
      },
      {
        "HR": 135
      }
    ]
  }
}

How can i formulate the query, to retrieve this kind of data from the json, and still return the json object, with only the specified paths?

Best Answer

You almost certainly can't.

JSON doesn't [yet] have the equivalent of XML's XSLTransform, which could do what you describe, but [still] only after retrieving the entire blob of XML to work with.

Databases are really good at finding little bits of stuff and putting them together; they're far less good and taking large blobs of stuff and pulling them apart.

I assume what you're showing is a simplification but what you've shown could be done easily with ordinary, relational tables.