[JSONPath select only leaves

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

我需要编写一个JSONPath表达式,该表达式选择具有给定名称的键的文本值。问题在于,相同的密钥存在于具有两个不同结构的文档中。 title键可以是简单的字符串或复杂的对象。

我只想选择文本值。用作输入的文档如下:

{
    "tool": {
        "jsonpath": {
            "creator": {
                "name": "Jayway Inc.",
                "location": [
                    "Malmo",
                    "San Francisco",
                    "Helsingborg"
                ],
                "title": {
                  "a": "value"
                }
            }
        }
    },
    "book": [
        {
            "title": "Beginning JSON",
            "price": 49.99
        },
        {
            "title": "JSON at Work",
            "price": 29.99
        }
    ]
}

如果尝试像这样的简单尝试:

$..title

结果是:

[
   {
      "a" : "value"
   },
   "Beginning JSON",
   "JSON at Work"
]

但是我想要的是:

[
   "Beginning JSON",
   "JSON at Work"
]

我尝试了以下表达式,但无法获得预期的结果:

$..title[?(@.length()==0)]
$..title[?(@.length()==null)]
$..title[?(@.length()=='null')]
$..title[?(!(@.*))]
$..title[?(@.*.empty())]
jsonpath json-path-expression
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.