I’m trying to retreive some specific data from a json stored in my database.
Here is my fidle : https://www.db-fiddle.com/f/5qZhsyddqJNej2NGj1x1hi/1
An exemple of a json string :
{
"complexProperties":[
{
"properties":{
"key":"Registred",
"Value":"123456789"
}
},
{
"properties":{
"key":"Urgency",
"Value":"Total"
}
},
{
"properties":{
"key":"ImpactScope",
"Value":"All"
}
}
]
}
In this case I need to retreive the value of Registred
which is 123456789
Here is the request I tried to retreive first all value:
SELECT CAST(data AS jsonb)::json->>'complexProperties'->'properties' AS Registred FROM jsontesting
Query Error: error: operator does not exist: text -> unknown
2
Answers
You can use a JSON Path expression:
This returns a
jsonb
value. If you need to convert that to atext
value, usejsonb_path_query_first(...) #>> '{}'
Online example
An alternative that first flattens the JSON field (the
arrj
subquery) and then performs an old-school select. Using yourjsontesting
table –Online example