0

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

3 Answers 3

2

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

  1. Switching to JSON JMESPath Extractor
  2. Sorting the array alphabetically
  3. Slicing the array to return only last match (USDT will be "lower" than SGD)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

nice dimtri, but what if YUN will be there ?
1

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

Comments

0

You can use the jsonpath-expression to check if the value exists in the array

$..currency[?(@ == "USDT")] 

jsonpath.com

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.