Pulling multiple values from JSON response using RegEx Extractor

前端 未结 4 1079
情话喂你
情话喂你 2020-12-03 23:00

I\'m testing a web service that returns JSON responses and I\'d like to pull multiple values from the response. A typical response would contain multiple values in a list. F

4条回答
  •  粉色の甜心
    2020-12-03 23:40

    I am assuming that JMeter uses Java-based regular expressions... This could mean no named capturing groups. Apparently, Java7 now supports them, but that doesn't necessarily mean JMeter would. For JSON that looks like this:

    {
    "name":"@favorites",
    "description":"Collection of my favorite places",
    "list_id":4894636,
    }
    
    {
    "name":"@AnotherThing",
    "description":"Something to fill space",
    "list_id":0048265,
    }
    
    {
    "name":"@SomethingElse",
    "description":"Something else as an example",
    "list_id":9283641,
    }
    

    ...this expression:

    \{\s*"name":"((?:\\"|[^"])*)",\s*"description":"((?:\\"|[^"])*)",(?:\\}|[^}])*}
    

    ...should match 3 times, capturing the "name" value into the first capturing group, and the "description" into the second capturing group, similar to the following:

    1                 2
    ---------------   ---------------------------------------
    @favorites        Collection of my favorite places
    @AnotherThing     Something to fill space
    @SomethingElse    Something else as an example
    

    Importantly, this expression supports quote escaping in the value portion (and really even in the identifier name portion as well, so that the Javascript string I said, "What is your name?"! will be stored in JSON as AND parsed correctly as I said, \"What is your name?\"!

提交回复
热议问题