Home > OS >  Jmeter extracting JSON format inside an Array
Jmeter extracting JSON format inside an Array

Time:05-30

I would like to seek an assistance how can I extract the value USDT on this sample response. Your response is highly appreciated. Currently this is how I extract my USDT using JSON Path tester $..currency[0]. I want to make it flexible without using number instead contains. is there's a way to simulate this? Thank you so much in advance

{ "uid": "123-321", "period": "25_minutes", "level": "symbol", "values": [ 1.3211, 1.2212 ], "rank_by": "volume", "currency": [ "USDT", "SGD" ], "measurements": [ 0.42, 0.15 ], "num_instruments": 20, "asset_classes": [ "All" ], "version": "1.0.0", "timestamp": "2022-05-30T03:53:09" }

Sample response I extracted:

enter image description here

CodePudding user response:

If you're uncertain about the order of currencies in the response you can consider

  1. Switching to enter image description here

    CodePudding user response:

    Dont have clue about JSON extractor, but can be done through the following method,

    1. Add JSR223 Postprocessor as a child of the request which returns above JSON
    2. Put the following code into "Script" area

    Script Block:

     def jsonResponse = new groovy.json.JsonSlurper().parse(prev.getResponseData())
        jsonResponse.each { getCurrencyArray ->
                def currencyExpected = "USDT"
                  if(getCurrencyArray.getKey() == "currency"){
                            getCurrencyArray.getValue().each { curr ->
                                if (curr.equalsIgnoreCase("USDT")) {
                                    vars.put("Cur", currencyExpected )
                                    log.info("Cur = "   currencyExpected)
                                }
                            }
                  }
        }
    

    With USDT:

    enter image description here

    With Multiple USDT: enter image description here

    Without USDT: enter image description here

  • Related