2

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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

@Dimitri, my bad. I forgot the comma. Also I meant my expression is $.books[?(@.author.lastName=='xyz')].title (sorry about repeating but I can't edit my question. don't know why). Here's what my thinking for my expression: 1. $.books --> get an array of book 2. ?() --> fileter 3. ?(@.author.lastName=='xyx') ----> pick author object's lastname and compare with xyz. However, it doesn't work. Looks like ?() only can take the first level object. IOW ?(@.title=='book 2 title') is fine. Anyways, thanks
3

Try below expression:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.