Forum Discussion

Jseban's avatar
Jseban
Occasional Contributor
8 years ago

JSONSLURPER - How to check optional array available in response and save value in property?

Hi,   Below is a sample JSON response.   { "uri": "VehiclesandGadgets", "Types": { "Vehicle": [{ "label": "Click to expand", "value": { "Cars": [{ "label": "Click to expand", "value": { ...
  • nmrao's avatar
    nmrao
    8 years ago

    Jseban,

     

    Ok, thank you for the details and patience.

     

    You can use below :

    //Pass json string to below parseText method
    def jsonRes = new groovy.json.JsonSlurper().parseText(json)
    //Get the make 
    def makes = jsonRes.Types?.Vehicle?.value?.Cars?.value?.Properties?.Make
    //Assign actual data, otherwise assign NA
    def result = makes ? makes.flatten()[0] : 'NA'
    
    log.info result
    
    //You can proceed using result variable do the stuff you needed to do.

     

    Also you can quickly try it online demo