0

I can't get a specific row from this JSON array. So I want to get the object where filed 'type' is equal to 'No-Data'

Are there exist any functions in SQL to take the row or some expressions?

 "metadata": { "value": "JABC" },
 "force": false
 "users": [
     { "id": "111", "comment": "aaa", type: "Data" },
     { "id": "222", "comment": "bbb" , type:"No-Data"},
     { "id": "333", "comment": "ccc", type:"Data" }
 ]
1
  • You json demo is not correct formed. see demo Commented Aug 11, 2022 at 5:40

1 Answer 1

1

You can use a JSON path query:

select jsonb_path_query_first(the_column, '$.users[*] ? (@.type == "No-Data")')
from the_table

This assumes that the column is defined as jsonb (which it should be). If it's not you have to cast it: the_column::jsonb

Online example

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

3 Comments

@a-horse-with-no-name also, can I extract only id from this json?
Just use ->> 'id' on the result. But your question was "to get the object" which is what I answered.
yeah, thank you a lot, I have figured out only now

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.