Henry Dang
Henry Dang

Reputation: 853

Jmeter - Json Path extractor - conditional selection

I'm trying to conditionally extract the value of an element from the following sample json response:

{
    "books": [
    {
        "title": "book 1 title",
        "author": {
            "firstName": "author01",
            "lastName": "abc"
        }
    }
    {
       "title": "book 2 title",
       "author": {
          "firstName": "author02",
          "lastName": "xyz"
       }
    }
   ]
}

I want to select the book title where lastName == xyz. Here the expression that I use: $.books[?(@.author.lastName=='xyz')]

but it returns []

Thanks

Upvotes: 2

Views: 3295

Answers (2)

Poorvaja Deshmukh
Poorvaja Deshmukh

Reputation: 129

Try below expression:

$.books[?(@.author.lastName=='xyz')].title

Upvotes: 3

Dmitri T
Dmitri T

Reputation: 167992

Your JSON Path expression is correct, demo:

JSON Path Conditional Select

however there is a problem with your JSON:

{
  "books": [
    {
      "title": "book 1 title",
      "author": {
        "firstName": "author01",
        "lastName": "abc"
      }
    }, <-----------------------------you need the comma here
    {
      "title": "book 2 title",
      "author": {
        "firstName": "author02",
        "lastName": "xyz"
      }
    }
  ]
}

If your response cannot be resolved to a valid JSON - you will have to use Regular Expression Extractor instead. If it is - double check your JSON Path expression and also worth checking out JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Upvotes: 3

Related Questions