Postgresql get keys from nested array of objects in JSONB field

Jitendra

Here' a dummy data for the jsonb column

{
"address": [
{
  "country": "US",
  "extension": [
    {
      "extension": [
        {
          "valueDecimal": -71.024638,
          "url": "latitude"
        },
        {
          "url": "longitude",
          "valueDecimal": 42.082543
        }
      ],
      "url": "url1"
    }
  ],
  "postalCode": "02301",
  "city": "Brockton"
},
{
  "country": "US",
  "extension": [
    {
      "extension": [
        {
          "valueDecimal": -71.024638,
          "url": "latitude"
        },
        {
          "url": "longitude",
          "valueDecimal": 42.082543
        }
      ],
      "url": "url2"
    }
  ],
  "postalCode": "02301",
  "city": "Brockton"
}
]
}

I want to get something like, address->'extension'->'extension'->'valueDecimal' but it only works with the json but it is an array of JSON data...my expecting output -

[-71.024638, -71.024638]

I'm able to get a first extension(i.e first array of JSON ) with the below query

SELECT elems.value  FROM "patient", jsonb_array_elements(resource -> 'extension') AS elems;

so the problem is that I'm not able to get inner 'extension'(It is inside the extension object) data. Any help will be Helpful.

Kaushik Nayak

You may cross join multiple levels of jsonb_array_elements

SELECT json_agg(elems3->>'valueDecimal') as latitudes
    FROM patient
 cross join jsonb_array_elements(resource ->'address'  ) AS elems
 cross join jsonb_array_elements(elems ->'extension')  AS elems2
 cross join jsonb_array_elements(elems2 ->'extension') AS elems3
 where elems3->>'url' = 'latitude'

DEMO

This may be slower for large records although there are ways to improve performance. A recommended option is to redesign and normalize your table to store values separately as columns in their corresponding tables and use JSON only where there's no other way to deal with the data.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Postgresql query array of objects in JSONB field

jsonb query with nested objects in an array

Searching nested jsonb array in PostgreSQL

get array of all json field keys in postgresql

jsonb LIKE query on nested objects in an array

Get elements as flat string from array of jsonb in postgresql

Postgresql get keys from array of objects in JSONB field

Nested search without keys in jsonb in PostgreSql

Flatten the jsonb nested array in postgresql

Get one element from jsonb array in PostgreSQL

Remove key from multiple nested jsonb objects with unknown keys in PostgreSQL

PostgreSQL jsonb - omit multiple nested keys

Build jsonb array from jsonb field

Remove Key Value pair from jsonb nested array in postgresql

Delete objects inside NESTED JSONB arrays with PostgreSQL

PostgreSQL query on a JSONB Array of Objects

How to get field from postgres jsonb nested array

Get an array from nested objects

How to get the value from a nested array with objects

Select specific field and get result from array in jsonb field

Is there a magic function with can extract all select keys/nested keys including array from jsonb

Get array from nested json value objects

Add objects inside NESTED JSONB arrays with PostgreSQL

Get nested value in JSONB column POSTGRESQL

How to get PostgreSQL to escape text from jsonb_array_element?

Postgresql migrate data from jsonb array to jsonb

Update object field of array in jsonb PostgreSQL

Postgresql remove object from jsonb array of objects by key value

get data from unlimited nested array of objects

TOP Ranking

HotTag

Archive