JsonPath 条件嵌套两个数组深

问题描述 投票:0回答:1

当 url = 'www.fruits.com' 时,我尝试返回完整的父对象

下面是 json 模式。还蛮棘手的

{
  "produce": [
    {
      "type": "fruit",
      "upc": {
        "code": {
          "codingList": [
            {
              "item": "apple",
              "organizations": [
                {
                  "url": "www.fruits.com"
                }
              ]
            }
          ]
        }
      }
    },
    {
      "type": "fruit",
      "upc": {
        "code": {
          "codingList": [
            {
              "item": "tomato",
              "organizations": [
                {
                  "url": "www.special-fruits.com"
                }
              ]
            }
          ]
        }
      }
    }
  ]
}

任何帮助将不胜感激。

我在 jsonpath.com 上尝试了几种不同的方法,但未能破解它。

jsonpath json-path-expression
1个回答
0
投票

JSON 路径不支持有条件地返回根值。获得根的唯一方法是使用路径

$
,该路径不允许条件。


假设您的意思是您想要

parents
中具有
url
且值为
www.fruits.com
的项目,您可以使用以下命令:

$.produce[?(@..[?(@.url == 'www.fruits.com')])]

您可以在 https://json-everything.net/json-path 上进行测试,它实现了即将推出的 JSON 路径 IETF 规范。

https://jsonpath.com/似乎不太支持

@
。看来它需要一个子查询,例如
@.foo

© www.soinside.com 2019 - 2024. All rights reserved.