Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude fields from JSON using JSONPath

Tags:

json

jsonpath

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

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')]

This gives me below response:

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

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.

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

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

Thanks in advance.

like image 542
Darshan Mehta Avatar asked Jan 07 '15 13:01

Darshan Mehta


People also ask

What is difference between JSON & JSONPath?

JSONPath creates a uniform standard and syntax to define different parts of a JSON document. JSONPath defines expressions to traverse through a JSON document to reach to a subset of the JSON. This topic is best understood by seeing it in action. We have created a web page which can help you evaluate a JSONPath.

Is there XPath for JSON?

JsonPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. The "root member object" in JsonPath is always referred to as $ regardless if it is an object or array.

Can you use wildcards in JSON?

IMPORTANT Assertible's JSON Path syntax supports wildcards in the path. Only single node selections are valid using pure json path. If you need more advanced scripting capabilities, use the . jq() function.


2 Answers

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']

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'.

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

like image 194
Duncan Avatar answered Nov 10 '22 14:11

Duncan


Using Jayways JsonPath (Java) this works:

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

Test different implementations here

like image 22
kalle Avatar answered Nov 10 '22 12:11

kalle