I have this JSON on my database:
{
"Id": 1,
"Questions": [
{
"QuestionId": 6,
"Description": "Question 1",
"Alternatives": [
{
"Index": 1,
"CorrectAnswer": false,
"AlternativeId": 26,
"QuestionId": 6,
"Description": "Alternative one",
"Selected": false
},
{
"Index": 2,
"CorrectAnswer": true,
"AlternativeId": 27,
"QuestionId": 6,
"Description": "Alternative two",
"Selected": false
}
]
},
{
"QuestionId": 7,
"Description": "Question 2",
"Alternatives": [
{
"Index": 1,
"CorrectAnswer": false,
"AlternativeId": 26,
"QuestionId": 6,
"Description": "Alternative one",
"Selected": false
},
{
"Index": 2,
"CorrectAnswer": true,
"AlternativeId": 27,
"QuestionId": 6,
"Description": "Alternative two",
"Selected": false
}
]
}
]
}
I can not get just ONE question in this document. I tried the following queries:
select data#>'{Questions}' from db.my_table
where (data #> '{Questions,0,QuestionId}')::numeric = 6;
SELECT data ->> 'Questions' AS Questions FROM db.my_table
WHERE (data -> 'Questions' ->> 'QuestionId')::numeric = 6;
SELECT data ->> 'Questions' AS Questions FROM db.my_table
WHERE data -> 'Questions' ->> 'QuestionId' = '6'
What can I be doing wrong? I always get the return of 0 affected rows.
Documentation:
https://www.postgresql.org/docs/current/static/datatype-json.html https://www.postgresql.org/docs/current/static/functions-json.html
Every help is welcome!