如何在JSONPath中按字符串过滤?

时间:2012-09-25 15:15:05

标签: json jsonpath

我有来自Facebook API的JSON响应,如下所示:

{
  "data": [
     {
       "name": "Barack Obama", 
       "category": "Politician", 
       "id": "6815841748"
     }, 
     {
       "name": "Barack Obama's Dead Fly", 
       "category": "Public figure", 
       "id": "92943557739"
     }]
 }

我想将JSONPath应用于它,只返回类别为“Politician”的结果。根据我的阅读,似乎我需要这样做:

$.data[?(@.category=='Politician')]

但根据我发现的testing tool,这不起作用。我发现another question表明我应该使用“eq”而不是“==”,但这也不起作用。我在这里弄错了什么?

5 个答案:

答案 0 :(得分:25)

您的查询看起来很好,您的数据和查询使用this JsonPath解析器为我工作。另请参阅该页面上的示例查询以获取更多谓词示例。

您正在使用的测试工具似乎有问题。即使JsonPath site中的示例返回的结果也不正确:

例如,给定:

{
    "store":
    {
        "book":
        [ 
            { "category": "reference",
              "author": "Nigel Rees",
              "title": "Sayings of the Century",
              "price": 8.95
            },
            { "category": "fiction",
              "author": "Evelyn Waugh",
              "title": "Sword of Honour",
              "price": 12.99
            },
            { "category": "fiction",
              "author": "Herman Melville",
              "title": "Moby Dick",
              "isbn": "0-553-21311-3",
              "price": 8.99
            },
            { "category": "fiction",
              "author": "J. R. R. Tolkien",
              "title": "The Lord of the Rings",
              "isbn": "0-395-19395-8",
              "price": 22.99
            }
        ],
        "bicycle":
        {
            "color": "red",
            "price": 19.95
        }
    }
}

表达式为$.store.book[?(@.length-1)].title,该工具会返回所有标题的列表。

答案 1 :(得分:5)

删除引号:

List<Object> bugs = JsonPath.read(githubIssues, "$..labels[?(@.name==bug)]");

另见Json Path Example page

答案 2 :(得分:3)

我没有找到正确的jsonpath过滤器语法来从json中的名称 - 值对中提取值。

下面是语法和缩写示例Twitter文档。

This site对测试非常有用:

jsonpath过滤器表达式:

.events[0].attributes[?(@.name=='screen_name')].value

测试文件:

{
  "title" : "test twitter",
  "priority" : 5,
  "events" : [ {
    "eventId" : "150d3939-1bc4-4bcb-8f88-6153053a2c3e",
    "eventDate" : "2015-03-27T09:07:48-0500",
    "publisher" : "twitter",
    "type" : "tweet",
    "attributes" : [ {
      "name" : "filter_level",
      "value" : "low"
    }, {
      "name" : "screen_name",
      "value" : "_ziadin"
    }, {
      "name" : "followers_count",
      "value" : "406"
    } ]
  } ]
}

答案 3 :(得分:0)

''Alastair,你可以使用这个lib和工具; DefiantJS。它在JSON结构上启用XPath查询,您可以测试和验证此XPath:

//data[category="Politician"]

DefiantJS使用“搜索”方法扩展全局对象,后者又返回带有匹配项的数组。在Javascript中,它看起来像这样:

var person = JSON.search(json_data, "//people[category='Politician']");
console.log( person[0].name );
// Barack Obama

答案 4 :(得分:0)

浏览器测试工具虽然方便但可能有点欺骗。考虑:

{
  "resourceType": "Encounter",
  "id": "EMR56788",
  "text": {
    "status": "generated",
    "div": "Patient admitted with chest pains</div>"
  },
  "status": "in-progress",
  "class": "inpatient",
  "patient": {
    "reference": "Patient/P12345",
    "display": "Roy Batty"
  }
}

大多数工具都将其返回为false:

$[?(@.class==inpatient)]

但是当我执行

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>1.2.0</version>
</dependency>

它返回true。我建议编写一个简单的单元测试来验证而不是依赖于浏览器测试工具。