How to get json value? skip first level?

My json file looks like:
//

“189”: {“value”:266,“label”:“External Coordinator Course (EXCOORD-HL-EN) (EXCOORD-HL-EN)”},
“162”:{“value”:239,“label”:“First Aid Course (PMP-HL-EN)”},
“54”:{“value”:82,“label”:“First Aid Course (FA-HL-EN) (FA-HL-EN)”}

//
I want to get value from field “value” if matches label. I tried:
//

def response = WS.sendRequest(findTestObject(object))

String atsakymas = response.getResponseBodyContent()

def list = new JsonSlurper().parseText( atsakymas )

Number label_value = (list.find{ it.label == labelis}).value

//
But I get error that label doesn`t exist.
Before katalon update my code was working.

My mistake, is not related with katalon update. So my question is how can I skip first level?

Anyone?HELP!

Edita,

Here are some examples of how to read JSON value: http://mrhaki.blogspot.com/2011/11/grassroots-groovy-reading-json-with.html
Hope that it helps.

No, that is not what I need. I need skip first value that mean I don`t know what first value can be because it is random number.

if the main list is a JSON array and the key is a random number, you can either select it randomly or use a for loop to get each element and choose the one you want.

I made a TestCase as a mimic of yours:

import groovy.json.JsonSlurper
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
def atsakymas = '''
{"189":{"value":266,"label":"External Coordinator Course (EXCOORD-HL-EN) (EXCOORD-HL-EN)"}},
{"162":{"value":239,"label":"First Aid Course  (PMP-HL-EN)"}},
{"54":{"value":82,"label":"First Aid Course (FA-HL-EN) (FA-HL-EN)"}}
'''
// The string contained in the variable atsakymas is NOT a valid JSON.
def list = new JsonSlurper().parseText( atsakymas )
WebUI.comment("list=${list}")
// '[' + atsakumas + ']' makes a valid JSON
def wrapped = new JsonSlurper().parseText('[' + atsakymas + ']')
WebUI.comment("wrapped=${wrapped}")
def expectedLabel = 'First Aid Course  (PMP-HL-EN)'
def wantedValue = 0
// Here I assume the 'wrappeed' varaiable is an instance of java.util.List<Map>
for (Map map : wrapped) {
    Set<String> keySet = map.keySet()
    Iterator iter = keySet.iterator()
    while (iter.hasNext()) {
        def key = iter.next()
        WebUI.comment("key=${key}")
        def v = map.get(key)
        if (v.get('label') == expectedLabel) {
            wantedValue = v.get('value')
            WebUI.comment("wantedValue=${wantedValue} when key=${key}")
        }
    }
}

When I run this, I got the following output:

wantedValue=239 when key=162

This should be what you wanted to achieve, isn’t it?

KatalonDiscussion6702.png

4 Likes

If I could, I would change the input data; it means changing your Web App itself. If the input JSON is in a usual format, then my test case code would be much simpler. Have a look at the following example.

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import groovy.json.JsonSlurper
def atsakymas = '''
[
    {"key":"189","content":{"value":266,"label":"External Coordinator Course (EXCOORD-HL-EN) (EXCOORD-HL-EN)"}},
    {"key":"162","content":{"value":239,"label":"First Aid Course  (PMP-HL-EN)"}},
    {"key":"54", "content":{"value":82,"label":"First Aid Course (FA-HL-EN) (FA-HL-EN)"}}
]
'''
def list = new JsonSlurper().parseText( atsakymas )
def expectedLabel = 'First Aid Course  (PMP-HL-EN)'
def wantedValue = 0
for (Map map : list) {
    if (map.content.label == expectedLabel) {
        wantedValue = map.content.value
        WebUI.comment("expectedValue=${wantedValue}")
    }
}

THANKS A LOT. VERY VERY MUCH!!! :slight_smile: