首页 > 解决方案 > 在 PostgreSQL JSON 中查找结果节点的路径

问题描述

我想执行 jsonpath 查询jsonb_path_query_all(..., '$.**.bar),例如

{
  "foo": [
   { "xxx": 24 }
   { "bar": 42 }
  ]
}

但不是只得到结果[42],我想找到结果的所有路径,所以在这种情况下它将是[ ["foo", 1 ] ]. 有没有一种在 JSON 中查找值“位置”的有效方法?

在 JS jsonpath 中有类似jp.pathsjp.nodes的方法可以做到这一点。

编辑:遗憾的是 Postgres 不支持$..barJSONPath 的许多实现支持的符号:

# SELECT jsonb_path_query('{"foo":{"bar":42}}'::jsonb, '$..bar');
ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
LINE 1: ...ECT jsonb_path_query('{"foo":{"bar":42}}'::jsonb, '$..bar');

标签: postgresqljsonbjsonpath

解决方案


推荐阅读