1

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"
]
  • It is working fine for Example 1...whereas for Example 2...it is retrieving value of innerBook1 category also...but ideally it should not. – Mohammed Abdullah Nov 29 '20 at 14:51
  • Yes...from Example 1...InnerBook1.category is ignoring but for Example 2 InnerBook1.category is including in response. – Mohammed Abdullah Nov 29 '20 at 15:32
  • Requirement is for both Examples ..need to retrieve only InnerBook category alone, because in Jpath Expression InnerBook1 not defined, because of Deep copy currently it is pulling value of innerBook1 category value also, Ideally it should not. I added Expected Results for Both Examples. Please look into it and if you find any solution, it will be really helpful – Mohammed Abdullah Nov 29 '20 at 15:43

1 Answers1

1

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.

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Thanks for the answer it is working fine for array object, but it is not working for Json object whereas innerBook is an Object not an array. Can you please provide Jpath expression to get both jsonobject as well as jsonarray; – Mohammed Abdullah Nov 16 '20 at 14:37
  • @MohammedAbdullah Glad to hear there is some progress; can you provide an updated sample to avoid confusion? – wp78de Nov 16 '20 at 17:20
  • Added JsonObject Example, can you please help out to retrieve value using single Jpath Expression. – Mohammed Abdullah Nov 16 '20 at 17:53
  • @MohammedAbdullah is this a theoretical question or do you use this somewhere? – wp78de Nov 16 '20 at 19:53
  • @MohammedAbdullah As hinted, I think you need to run two queries to handle varied input. The following filter could help you to determine if query 1 or 2 should be used: `$.store..book..[?(@.innerBook[1].category empty false)]` – wp78de Nov 29 '20 at 16:29
  • So its not possible to run in Single query, In My Current Application I can pass only one query(One JpathExpression) per Attribute. – Mohammed Abdullah Nov 29 '20 at 16:33
  • Maybe someone else can suggest a oneliner but I don't see it right now. I might give it another try. – wp78de Nov 29 '20 at 16:41