简体   繁体   中英

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

Your JSON Path expression is correct, demo:

JSON路径条件选择

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

Try below expression:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM