简体   繁体   中英

How to parse JSON array objects to string array objects in JMeter

I have the following JSON. I want the JSON array objects of technicalSettings (two objects in this case but can vary based on the API response) into a string array without loosing any text and want to loop through the string array to add few more elements and to form a new JSON and store the new JSON in a string variable.

{
  "data": {
    "statusCode": 200,
    "success": true,
    "technicalSettings": [
      {
        "program": "C:/temp/abc.exe",
        "actions": "9",
        "file_name": "abc1",
        "new_file_name": "newabc1",
        "version": "2.0.0.0",
        "product_name": "abc",
        "description": "abc",
        "eventdate": "20160601120000",
        "autoVoiceProfile": {
              "autoVoices": [
                {
                  "autoVoiceLanguage": 0,
                  "autoVoiceMessage": [
                    {
                      "name": "AV1",
                      "duration": "1.200000",
                      "checksum": "2d4c44d142bc0391b980b8a103ab35cc23d8f7820895cb6025cf3c829139336c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg7.aifc",
                      "id": 4
                    },
                    {
                      "name": "AV1",
                      "duration": "0.600000",
                      "checksum": "9538cf287d178964dcb57a05b7acbc00e04c800a9aaed0b22f5433d9dc79d80c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg8.aifc",
                      "id": 4
                    },
                    {
                      "name": "AV2",
                      "duration": "2.800000",
                      "checksum": "050acdb345e079da1371623c9727bc16d166db0a0b47687ff93d736ddf37cde8",
                      "fileName": "/usr/g/db/user_autoVoiceMsg9.aifc",
                      "id": 5
                    },
                    {
                      "name": "AV2",
                      "duration": "4.100000",
                      "checksum": "c5a6a39df38505c0c22b75d9ea7781a1755e9c8c9f435e08034f579361ba751c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg10.aifc",
                      "id": 5
                    }
                  ]
                }
              ],
              "messagesitefilename": null
            }
        
      },
      {
        "program": "C:/temp/abc.exe",
        "actions": "9",
        "file_name": "abc2",
        "new_file_name": "newabc2",
        "version": "2.0.0.0",
        "product_name": "abc",
        "description": "abc",
        "eventdate": "20160601120000",
        "autoVoiceProfile": {
              "autoVoices": [
                {
                  "autoVoiceLanguage": 0,
                  "autoVoiceMessage": [
                    {
                      "name": "AV1",
                      "duration": "1.200000",
                      "checksum": "2d4c44d142bc0391b980b8a103ab35cc23d8f7820895cb6025cf3c829139336c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg7.aifc",
                      "id": 4
                    },
                    {
                      "name": "AV1",
                      "duration": "0.600000",
                      "checksum": "9538cf287d178964dcb57a05b7acbc00e04c800a9aaed0b22f5433d9dc79d80c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg8.aifc",
                      "id": 4
                    },
                    {
                      "name": "AV2",
                      "duration": "2.800000",
                      "checksum": "050acdb345e079da1371623c9727bc16d166db0a0b47687ff93d736ddf37cde8",
                      "fileName": "/usr/g/db/user_autoVoiceMsg9.aifc",
                      "id": 5
                    }
                  ]
                }
              ],
              "messagesitefilename": null
            }
      }
    ],
    "library": {
      "version": 6,
      "dmIdVersion": 5
    }
  },
  "success": true,
  "statusCode": 200,
  "errorMessage": ""
}

I used the JSON Extractor but it is failing when split into array since the array objects contains multiple ",".

String strPublishTechSettings = "${pPublishTechSettings_ALL}";

String[] PublishTechSettings = strPublishTechSettings.split(",");

在此处输入图像描述

Don't inline JMeter Functions or Variables into scripts as:

  • in case of compilation caching enabled only first value will be used for all iterations
  • it conflicts with Groovy GString Template Feature
  • it might be resolved into something causing compilation failure or unexpected behaviour

so change this line:

String strPublishTechSettings = "${pPublishTechSettings_ALL}";

to this one:

String strPublishTechSettings = "${pPublishTechSettings_ALL}";

and your test should start working as expected在此处输入图像描述 :

in the above example vars stands for JMeterVariables class instance, see JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on other JMeter API shorthands available to JSR223 Test Elements

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