3

I was finding the query that fetch the data from table of column which of json type.

My table looks like:

`ID | ParentID |        Details`

`-------------------------------------------------------------------------------------------`

`1  |  10      | {"_translated": {"en_US": {"is_draft": "false"}, "default_locale": "en"}}`

`2  |  20      | {"_translated": {"en_US": {"is_draft": "true"}, "default_locale": "en"}}`

`3  |  30      | {"_translated": {"en_CA": {"is_draft": "true"}, "default_locale": "en"}}`

`4  |  40      | {"_translated": {"en_CA": {"is_draft": "false"}, "default_locale": "en"}}`

I want to fetch those row whose is_draft = false.

Please someone can help me this query.

2 Answers 2

2

Given that we don't even know which keys would have child JSON objects with is_draft set to false, one option here would be to just cast the JSON to text and search it using LIKE:

SELECT *
FROM yourTable
WHERE Details::text LIKE '%"is_draft": "false"%';

Demo

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

Comments

0

Try:

SELECT 
   id,
   parent_id, 
   details
FROM books
WHERE details->'_translated' -> 'en_US' -> 'is_draft' ='false' 
OR details->'_translated' -> 'en_CA' -> 'is_draft' ='false' ;

Comments

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.