How to capture nested JSON object values and group them based on specific condition in Jmeter

53 views Asked by At

Response:

[ { "rid": 2016, "userid": "abcd", "cid": "xyz", , "rname": "ijk", "des": null, "con": [{ "fname": "amnt_type", "op": "eqto", "vtype": "ENUM", "values": [{ "v1": "I", "v2": null, }, ], }, { "fname": "usd", "op": "GT", "vtype": "NUMERIC", "values": [{ "v1": "100.00", "v2": null } ] }, { "fname": "bcd", "op": "GT", "vtype": "ENUM", "values": [{ "v1": "101", "v2": null }, { "v1": "102", "v2": null }, { "v1": "678", "v2": null }, { "v1": "832", "v2": null }

            ]
        }, {
            "fname": "bcd",
            "op": "GT",
            "vtype": "ENUM",
            "values": [{
                    "v1": "101",
                    "v2": null
                }, {
                    "v1": "678",
                    "v2": null
                }, {
                    "v1": "832",
                    "v2": null
                }

            ]
        }, {
            "fname": "pcd",
            "op": "EQ",
            "vtype": "ENUM",
            "values": [{
                    "v1": "ATM",
                    "v2": null
                }, {
                    "v1": "KLX",
                    "v2": null
                }, {
                    "v1": "MKH",
                    "v2": null
                }

            ]
        }
    ]
}, {
    "rid": 876,
    "userid": "abcd",
    "cid": "xyz", ,
    "rname": "Rule2",
    "des": null,
    "con": [{
            "fname": "amnt_type",
            "op": "GT",
            "vtype": "ENUM",
            "values": [{
                    "v1": "I",
                    "v2": null,
                },
            ],
        }, {
            "fname": "bcd",
            "op": "EQ",
            "vtype": "ENUM",
            "values": [{
                    "v1": "685",
                    "v2": null
                }, {
                    "v1": "657",
                    "v2": null
                },

            ]
        },
    ]
}, {
    "rid": 9875,
    "userid": "abcd",
    "cid": "xyz", ,
    "rname": "PQR",
    "des": null,
    "con": [{
            "fname": "amnt_type",
            "op": "GT",
            "vtype": "ENUM",
            "values": [{
                    "v1": "I",
                    "v2": null,
                },
            ],
        },
    ]
}

]

ExpectedOutput:

{ { "rname": "ijk", "rid": 2016, "con": [{ "fname": "amnt_type", "op": "eqto", "vtype": "ENUM", "value": [ "I"

            ],
        }, {
            "fname": "usd",
            "op": "GT",
            "vtype": "NUMERIC",
            "value": [
                "100.00"

            ]

        }, {
            "fname": "bcd",
            "op": "GT",
            "vtype": "ENUM",
            "value": [
                "101", "102", "678", "832"

            ]
        }, {
            "fname": "pcd",
            "op": "EQ",
            "vtype": "ENUM",
            "value": [
                "ATM", "KLX", "MKH"

            ]
        }
    ]
}, {
    "rid": 876,
    "rname": "Rule2",
    "con": [{
            "fname": "amnt_type",
            "op": "GT",
            "vtype": "ENUM",
            "value": ["I"

            ],
        }, {
            "fname": "bcd",
            "op": "EQ",
            "vtype": "ENUM",
            "value": [
                "685", "657"

            ]
        }
    ]
}, {
    "rid": 9875,
    "userid": "abcd",
    "cid": "xyz", ,
    "rname": "PQR",
    "des": null,
    "con": [{
            "fname": "amnt_type",
            "op": "GT",
            "vtype": "ENUM",
            "value": ["I"

            ],
        },
    ],
},
}

Basically we need to capture all values under the values section and keep only value1 and combine it to it one array.

I am able to get rid,rname,con together but not able to achieve the value parameter.

I tried using JSON extractor, regular expression extractor still not able to get the output in desired format. Please help.

I am using groovy and Jmeter 5.0

1

There are 1 answers

1
Dmitri T On

If I correcltly got your requirement you could do something like:

def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())

response.each { entry ->
    entry.con.each { con ->
        def value = con.values.collect { value -> value.v1 }
        con.remove('values')
        con.put('value', value)
    }
}
vars.put('someVariable', new groovy.json.JsonBuilder(response).toPrettyString())

Put the code to the JSR223 PostProcessor which needs to be a child of the Sampler which returns the original JSON

The generated JSON can be accessed as ${someVariable} where required

More information: