Home > Net >  Extract nested JSON content from JSON with use of JSON Extractor in jMeter
Extract nested JSON content from JSON with use of JSON Extractor in jMeter

Time:07-06

I have JSON content inside another JSON that I need to extract as it is, without parsing its contents:

{
    "id": 555,
    "name": "aaa",
    "JSON": "{\r\n  \"fake1\": {},\r\n  \"fake2\": \"bbbb\",\r\n  \"fake3\": \"eee\" \r\n}",
    "after1": 1,
    "after2": "test"
}

When I use JSON Extractor with JSON Path expression:

$.JSON

It returns:

"{  
    "fake1": {},  
    "fake2": "bbbb",  
    "fake3": "eee"
}"

when I need to get the raw string:

"{\r\n  \"fake1\": {},\r\n  \"fake2\": \"bbbb\",\r\n  \"fake3\": \"eee\" \r\n}"

CodePudding user response:

console.log(JSON.stringify(data.JSON))

Here data is your given JSON data. At first, you have to extract your JSON/data. Then you have to stringify the JSON data using JSON.stringify(). The confusing fact you have done here is that you named your key in the JSON object as "JSON". In js when you extract a JSON object if there is another nested JSON object you will always get JSON data by just data.key_name where data is JSON data key is for Nested JSON key

CodePudding user response:

I think you need to switch to JSR223 PostProcessor instead of the JSON Extractor and use the following code:

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

vars.put('rawString', org.apache.commons.text.StringEscapeUtils.escapeJson(json))

You will be able to refer the extracted value as ${rawString} where required.

More information:

  • Related