How to modify json using JRS223 preprocessor in jmeter

Viewed 44

Json body looks like this:

{
    "access_key": "",
    "erid": "",
    "ch_sms": {
        "messages": [
            {
                "urlsh": false,
                "sp": "123",
                "vp": 20,
                "heid": 1,
                "teid": ,
                "peid": ,
                "tmid": 4,
                "msg": {
                    "txt": "hello"
                },
                "da": [
                    {
                        "number": "97278",
                        "cc": "IN",
                        "uid": "uid1",
                        "tags": [
                            "key",
                            "Value"
                        ]
                    }
                    
                ]
                
            }
        ],
        "metadata": {
            "chver": "1.0",
            "cburl": "",
            "heid": 1,
            "teid": ,
            "peid": ,
            "tmid": 4,
            "Oa": "",
            "flash": false,
            "tags": [
                "key",
                "tag1"
            ]
        }
    }
}

We need to add multiple "da" sections based on user input. For example if I give input from a CSV file as 3 then "da" section will repeat 3 times and it will look like as follow:

{
    "access_key": "",
    "erid": "",
    "ch_sms": {
        "messages": [
            {
                "urlsh": false,
                "sp": "123",
                "vp": 20,
                "heid": 1,
                "teid": ,
                "peid": ,
                "tmid": 4,
                "msg": {
                    "txt": "hello"
                },
                "da": [
                    {
                        "number": "97278",
                        "cc": "IN",
                        "uid": "uid1",
                        "tags": [
                            "key",
                            "Value"
                        ]
                    },
                    {
                        "number": "97278",
                        "cc": "IN",
                        "uid": "uid1",
                        "tags": [
                            "key",
                            "Value"
                        ]
                    },
                    {
                        "number": "97278",
                        "cc": "IN",
                        "uid": "uid1",
                        "tags": [
                            "key",
                            "Value"
                        ]
                    }
                    
                ]
                
            }
        ],
        "metadata": {
            "chver": "1.0",
            "cburl": "",
            "heid": 1,
            "teid": ,
            "peid": ,
            "tmid": 4,
            "Oa": "",
            "flash": false,
            "tags": [
                "key",
                "tag1"
            ]
        }
    }
}
1 Answers

You can try this,

import org.json.simple.JSONObject

JSONObject data = new JSONObject() 

data.put("number", "97278")
data.put("cc", "IN")
data.put("uid", "uid1")
data.put("tags", ["key","Value"])

int da = Integer.parseInt(vars.get("da"))

ArrayList<Object> listData = new ArrayList<Object>()

for(int i=0; i < da; i++)
{
    listData.add(data.toString())
}

log.info("da: " + listData )

Result log: da: [{"cc":"IN","number":"97278","uid":"uid1","tags":["key","Value"]}, {"cc":"IN","number":"97278","uid":"uid1","tags":["key","Value"]}, {"cc":"IN","number":"97278","uid":"uid1","tags":["key","Value"]}]

enter image description here

Related