How to filter rows on nested values in a json column?

风格不统一 提交于 2019-12-05 18:10:26

Your query is close. json_each() is the key function. Or jsonb_each() for jsonb. A couple of improvements:

SELECT *
FROM   things t
WHERE  EXISTS (
   SELECT FROM json_each(t.blueprint) b
   WHERE  b.value->>'name' ILIKE 'azamund'
   );

Old sqlfiddle
db<>fiddle here

Alternative with JSON array

You have already seen my related answer for JSON arrays:

While the query for nested JSON objects seems just as simple, there is superior index support for the array:


May get simpler / more efficient with SQL/JSON in Postgres 12 ...

Closest query I can execute (that returns data I need) is:

select *
from (select id, (json_each(blueprint)).value::json->>'name' as name
      from stocks) as t
where t.name ~* 'azamund';

Well... Maybe there is something better?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!