Nirmal Baral
Nirmal Baral

Reputation: 33

Extract an element value in json using jsonpath

Following is my json data

{
    "primary": [
        {
            "secondary": {
                "name": {
                    "fullname": "fullname1"
                },
                "alias": "alias1"
            },
            "reference": "reference1"
        },
        {
            "secondary": {
                "name": {
                    "fullname": "fullname2"
                },
                "alias": "alias2"
            },
            "reference": "reference2"
        }
    ]
}

Now I want to extract "alias" value based on condition based fullname value from this json data using jsonpath.

I am using following expression but failed to parse result

$.primary[*].secondary[?(@.name.fullname == "fullname1")].alias

Upvotes: 0

Views: 362

Answers (1)

Dmitri T
Dmitri T

Reputation: 168122

Your JSON is malformed, if it really looks like you posted - you won't be able to use JSON Extractor, you will have to switch to Regular Expression Extractor.

Valid JSON would be something like:

{
  "primary": [
    {
      "secondary": {
        "name": {
          "fullname": "fullname1"
        },
        "alias": "alias1"
      },
      "reference": "reference1"
    },
    {
      "secondary": {
        "name": {
          "fullname": "fullname2"
        },
        "alias": "alias2"
      },
      "reference": "reference2"
    },
    {
      "type": "online"
    }
  ]
}

And if it is correct your JsonPath expression works fine as it evidenced by JsonPath Tester mode of the View Results Tree listener:

enter image description here

Upvotes: 2

Related Questions