Search code examples
jsonjmeterjsonpath

JSON Path PostProcessor: storing JSON arrays and objects loses their data type


I'm using JSON Path PostProcessor with path expressions to store a JSON object from a JSON response, but when I later retrieve the variable, it has been reduced to string with key - pair values. So I don't know that was a string or number.

Example: Response looks like this

{
.
.
"currency" : {
                "code" : "AUD",
                "name" : "Australian Dollars",
                "symbol" : "$"
            },
.
}

Using the path expression, I find currency and save it. However, when I use it in a HTTP Request body data ("currency" : ${currency},), it comes like this:

"currency" : {code=AUD, name=Australian Dollars, symbol=$},

How do I get the JSON Path PostProcessor to save the JSON object 'as is" without losing the data type details? Should I be using a different approach instead of JSON Path?


Solution

  • I would recommend switching to JSR223 PostProcessor and Groovy language, this way you will have full control of what is going on and be able to do what you want.

    Assuming you have the following JSON response:

    {
      "currency": {
        "code": "AUD",
        "name": "Australian Dollars",
        "symbol": "$"
      }
    }
    

    the relevant Groovy coode will be something like:

    def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
    def currency = json.currency
    def currencyString = new groovy.json.JsonBuilder(currency).toPrettyString()
    vars.put('currency', currencyString)
    log.info(currencyString)
    

    Demo:

    JMeter Groovy JSON

    References: