user21448519
user21448519

Reputation:

JSONPath: Receiving parent after query on children?

I'm currently struggling on using JSONPath on the following file:

[        
    {
        "number": "5422",
        "sender": [
            {
                "id": "6001"
            },
            {
                "id": "7371"
            }
        ],
        "receiver": [
            {
                "id": "6205"
            },
            {
                "id": "3803"
            },
            {
                "id": "5601"
            }
        ]
        
    }    
]

I want to query the id's of sender and receiver (of multiple entries) and I want to receive the number in the header if and only if the parameters handed match with one of the ids in sender and receiver respectively. All of this using JSONPath, the file can not be modified.

After searching the forum and the official github page for JSONPath I tried and failed with the following solution:

$[?(@.sender.id == 6001 && @.receiver.id == 6205)].number

The online evaluator just returns "no match". Trying out another example from another query JSONPath get the id of a parent element by a sub-child value

$[?(@.sender['id'] == '6001')].number

returns the same. I see that there's a difference in the data structure, with mine having square brackets in sender & receiver, I assume that the error in my query lies there but I'm not finding a path to the correct solution.

If all you're going to type is "Json Path does not support Parent" then refrain. Other solutions for similar problems exist within it.

Thanks in advance.

Upvotes: 0

Views: 93

Answers (1)

Jerry Jeremiah
Jerry Jeremiah

Reputation: 9643

What you are looking for is:

$[?(@.sender[?(@.id == 6001)] && @.receiver[?(@.id == 6205)])].number

You have multiple arrays inside of each other so you need to filter each one separately.

Different websites work differently - some accept multiple level array filters and some don't

So maybe that means different jsonpath libraries also differ.

Upvotes: 0

Related Questions