1

i have a query that returns a json list:

 select type.value -> 'components' as json_data
   from manifests m left join
        jsonb_array_elements(m.json_data->'type') as type on true   

this returns the following data

[{"name": "xxx", "version": "1.1"}, {"name": "xxx2", "version": "1.2"}]

i can proceed it further if i use it as a text like:

 select *
   from jsonb_array_elements(
  '[{"name": "xxx", "version": "1.1"}, {"name": "xxx2", "version": "1.2"}]' ) 

gives

 {"name": "xxx", "version": "1.1"}
 {"name": "xxx2", "version": "1.2"}

but how i can reach the same using the first query? i cannot make it work using a subquery

update: my original data looks like this:

{
    "lastUpdated": 1569393969656,
    "type": [
        {
            "components": [
                {
                    "name": "xxx",
                    "version": "1.1"
                },
                {
                    "name": "xxx2",
                    "version": "1.2"
                }
            ],
            "description": "xxx"
        }
    ]
}

thanks

1 Answer 1

1

You can apply jsonb_array_elements() function twice :

select jsonb_array_elements(elm) as json_data 
  from
  (
   select jsonb_array_elements(json_data -> 'type') -> 'components' as elm 
     from manifests ) q

to be able to extract the related array elements in the subquery.

Demo

Sign up to request clarification or add additional context in comments.

5 Comments

Query execution failed Reason: SQL Error [22023]: ERROR: cannot extract elements from an object
my original table does not hold data like that, that is already a result of a transformation (see first query)
i need a solution which uses the first query
@clairvoyant can you share the original content of json_data directly?
i changed parcel to type to avoid any confusion for others

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.