简体   繁体   中英

Jayway JSONPath Expession for Both Object and Array

I am having to trouble to retrieve value if parent element can be JSONArray as well as JSONObject. For that I used Deepcopy syntax to retrieve it. Now Problem is am getting additional values if child attribute exists in Inner Array also.

Eg: JpathExpression:

$.store..book..innerBook..category

Result is:

[
   "innerReference1",
   "innerBook1Ref1",
   "innerReference2"
]

Example 1 Expected Result is:

[
   "innerReference1",
   "innerReference2"
]

Example 1:

{
    "store": {
        "book": [
        {
                "innerBook": [
                    {
                        "category": "innerReference1",
                        "author": "Nigel Rees",
                        "innerBook1": [
                            {
                                "category": "innerBook1Ref1"
                            }
                        ]
                    },
                    {
                        "category": "innerReference2",
                        "author": "Nigel Rees"
                    }
                ]
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

Example 2:

{
    "store": {
        "book": [
        {
                "innerBook": 
                    {
                        "category": "innerReference1",
                        "author": "Nigel Rees",
                        "innerBook1": [
                            {
                                "category": "innerBook1Ref1"
                            }
                        ]
                    }
                
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

Example 2 Expected Result is:

[
   "innerReference1"
]

A little late, however, we do not need to use recursive descent .. after innerBook to walk the items in the array; with Jayway's we can use innerBook[*].category to retrieve the desired result for example 1:

$.store..book..innerBook[*].category

Example 2 requires to remove the array accessor:

$.store..book..innerBook.category 

You cannot have innerBook as array and object in one JSON since it wouldn't be valid.

If you have an API (or so) that serves JSON with an innerbook property of array or object type (which would be a badly designed API since the whole point of an API is to have expected responses) you could either query both paths or make the access of categories depending on the existence of innerBook1 :

$.store.book..innerBook[?(@.innerBook1)]..category

Which only returns for both examples

[
   "innerReference1",
   "innerBook1Ref1"
]

Try it online here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM