0
votes

I'm trying create a json expression path that returns the id when the reference with {" id ":" 00000000000000000000000004640254 "}. I have tried with

$.[?(@.Relationship[?(@.Ref.id=='00000000000000000000000004640254')])].id

but it doesn't return data

Json message is

[
    {
        "id": "234567890234567890",
        "Relationship": [
            {
                "type": "Indirect",
                "Ref": {"id": "00000000000000000000000004640253_01"}
            },
            {
                "type": "Direct",
                "Ref": {"id": "00000000000000000000000004640254"}
            }
        ],
        "Specification": {"id": "Gold123AS"}
    },
    {
        "id": "234567890234567891",
        "Relationship": [
            {
                "type": "Indirect",
                "Ref": {"id": "00000000000000000000000004640253_02"}
            },
            {
                "type": "Direct",
                "Ref": {"id": "00000000000000000000000004640253"}
            }
        ],
        "Specification": {"id": "Gold123AS"}
    }
]

if someone can help me, thanks

1
I don't believe this is possible with jsonpath; I'll be happy to be proven wrong... One way to handle it is convert to xml.Jack Fleeting
Please elaborate what data you are looking for from the JSON. $..[?(@.id=='234567890234567891')] - This will give you the elements against the ID passed. $..[?(@.id=='234567890234567891')]..['type']- This will give you the type related to the id passed. $..[?(@.id=='234567890234567891')]..['id'] - This will give you all the ID present for the array.rajesh

1 Answers

0
votes

I don't think you can do this using JSON Extractor as returning the parent node is not implemented by underlying JsonPath library

Go for JSR223 PostProcessor and Groovy language instead, the relevant code would be something like:

def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())

0.upto(json.size() -1 , { idx ->
    if (json.get(idx).Relationship.find { it.Ref.id.equals('00000000000000000000000004640254') } != null) {
        vars.put('id', json.get(idx).id as String)
        return
    }
})

More information: