使用JSONPath从JSON排除字段 [英] Exclude fields from JSON using JSONPath

查看:928
本文介绍了使用JSONPath从JSON排除字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从REST服务调用中收到JSON响应,并且只想从响应中选择某些字段.我正在使用JSONPath过滤出字段.下面是JSON示例:

I am getting a JSON response from REST service call and want to select only some of the fields from response. I am using JSONPath to filter out the fields. Below is the JSON example:

{
    "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
        }
    }
}

例如我想从响应中选择类别为引用"的作者和标题.我正在使用下面的JSONPath

E.g. I want to select author and title from the response where category is 'reference'. I am using the below JSONPath

$.store.book[?(@.category='reference')]

这给了我以下答复:

{
    "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": 8.95
}

但是,我不需要所有字段.我只想要作者和标题.如果我尝试$.store.book[?(@.category='reference')]['author'],它会给我作者姓名,但是如果我尝试$.store.book[?(@.category='reference')]['author', 'title'],它不会返回任何东西.

However, I don't want all the fields. I only want author and title. If I try $.store.book[?(@.category='reference')]['author'], it gives me author name but if I try $.store.book[?(@.category='reference')]['author', 'title'], it doesn't return anything.

JSONPath中是否有任何条款可以选择(或排除)有条件或无条件的字段?

Is there any provision in JSONPath to select (or exclude) fields with or without condition?

我正在使用 http://jsonpath.curiousconcept.com/测试JSONPath.

I am using http://jsonpath.curiousconcept.com/ to test JSONPath.

谢谢.

推荐答案

您的帖子没有说明您使用的是Goessner还是Flow Communications JSON Path表达式求值器.两者都可以在您正在使用的表达测试器站点上找到.如果您使用的是Goessner,则可以使用以下查询,但不能在正在使用的表达测试网站上使用

Your post doesn't say whether you are using Goessner or Flow Communications JSON Path expression evaluator. Both are available on the expression tester site you are using. If you are using Goessner the following query works, but not on the expression testing site you are using

$.store.book[?(@.category=='reference')]['author','title']

请注意双等号(@.category=='reference')而不是单个等号.另外,选择字段'author','title'的逗号后不应有空格.

Note the double equal sign (@.category=='reference') instead of a single one. Also, there should be no space after the comma where selecting the fields 'author','title'.

您可以看到该表达式在这里起作用 http://www.jsonquerytool.com/sample/jsonpathselectmultiplefields

You can see the expression working here http://www.jsonquerytool.com/sample/jsonpathselectmultiplefields

这篇关于使用JSONPath从JSON排除字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆