I'm storing a lot of json from some websites on PostgreSQL 9.5 in the jsonb format. One of the fields is an array of Jsons that have the same information. Here is an example.
[
{
"text": "JIMIN",
"indices": [
16,
22
]
},
{
"text": "PUMA",
"indices": [
23,
28
]
}
]
I want to retrieve all the text
values, that is, I want to make a query that gives me JIMIN
and PUMA
on this example. I can't find a way to accomplish that with the commons operators (like #>>
). Is there a way to accomplish this?
Best Answer
You didn't provide any code, so I will create a sample table with one row and some queries to extract the information you need.
Load data:
To extract a specific item, let's say the
text
field of the second array item:This will output:
In order to extract all the
text
fields inside the array, a possible solution is:The output is:
Obviously this works with many records: each array item will be ported to one row for each array. This query is compatible with PostgreSQL 9.5. Look at the official docs to create a more efficient or versatile query ( https://www.postgresql.org/docs/9.5/static/functions-json.html ).