I searched quite a lot on this and is still irrefutable. I am using PostgreSQL. The column name is “sections” and the column type is json [] in the example below.
My column looks like this in the database:
sections [{"name" : "section1", "attributes": [{"attrkey1": "value1", "attrkey2": "value2"}, {"attrkey3": "value3", "attrkey4": "value4"}] }, {"name" : "section2", "attributes": [{"attrkey3": "value5", "attrkey6": "value6"}, {"attrkey1": "value7", "attrkey8": "value8"}] }]
This is a json array, and I want to get "attrkey3" in my result. To get a specific key from Json, I can use json_extract_path_text(json_column, 'json_property') , which works fine. But I have no idea how to get any property from json [].
If I talk about the example above, I want to get the value of the "attrkey2" property, which will be shown in my result. I know this is an array, so it may work differently than usual. all the values of my array will act as another string, so I may have to write a subquery, but I don’t know how to do it.
In addition, I cannot write the index statically and get the property of the json element from a specific index. My request will be generated dynamically, so I will never know how many elements are inside the json array.
I saw some static examples, but I don’t know how to implement them in my case. Can someone tell me how to do this in a request?