jmeterjson-path-expressionjsonslurper

Jmeter - How to get nested object in json with multiple object


I have this json:

{
    "deviceId": "deviceCustom",
    "moduleId": "custom",
    "properties": {
        "desired": {
            "settings": {
                "ef78c18c-2291-4d15-ae87-d89abb9b1fef": {
                    "name": "elements",
                    "version": "1.0.0",
                    "category": "A1"
                },
                "f4b04c94-4643-4b13-b10c-9a00fbf4ea27": {
                    "name": "tags",
                    "version": "2.0.0",
                    "category": "B1"
                }
            }
        }
    }
}

and I would like to get separately all the objects under "settings". E.g:

settings_1="f4b04c94-4643-4b13-b10c-9a00fbf4ea27":{"name":"tags","version":"2.0.0","category":"B1"}
settings_2="ef78c18c-2291-4d15-ae87-d89abb9b1fef":{"name":"elements","version":"1.0.0","category":"A1"}
settings_matchNr=2

In Jmeter I've configured a JSON Extractor with this JSON Path expression: $.properties.desired.settings but I got this result:

settings_1={"f4b04c94-4643-4b13-b10c-9a00fbf4ea27":{"name":"tags","version":"2.0.0","category":"B1"},"ef78c18c-2291-4d15-ae87-d89abb9b1fef":{"name":"elements","version":"1.0.0","category":"A1"}}
settings_matchNr=1

I've also tried to use JSR223 Post Processor with Slurper but no valid result. Could you help me on that?

Thanks in advance.


Solution

    1. Add JSR223 PostProcessor as a child of the request which returns the above JSON

    2. Put the following code into "Script" area:

      new groovy.json.JsonSlurper().parse(prev.getResponseData()).properties.desired.settings.entrySet().eachWithIndex { entry, index ->
          def setting = [:]
          setting.put(entry.getKey(), entry.getValue())
          vars.put('setting_' + (index + 1), new groovy.json.JsonBuilder(setting).toPrettyString())
      }
      
    3. That's it, you will be able to refer the extracted JSON Objects as ${setting_1} and ${setting_2}

    More information: