Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreQL fetch data from Json field which of type multidimensional array

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.

like image 331
Pankaj K Avatar asked Dec 04 '25 00:12

Pankaj K


1 Answers

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

like image 89
Tim Biegeleisen Avatar answered Dec 05 '25 14:12

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!