Forum Discussion

tsjakamaka's avatar
tsjakamaka
Occasional Contributor
5 years ago

Getting data out of json response

Hi,

 

As result of a GET call of REST service, I have following JSON response.

 

{
"result": [
{
"role": "ADMIN",
"relationNumber": 8046017,
"individual": {
"firstName": "aaaa",
"lastName": "bbbb",
"birthDate": "1980-09-03"
},
"addresses": [{
"street": "blablabla",
"houseNbr": "0014"
}],
"involvedObjects": []
},
{
"role": "VIEWER",
"relationNumber": 8046018,
"individual": {
"firstName": "cccc",
"lastName": "dddd",
"birthDate": "1980-09-03"
},
"addresses": [{
"street": "blablabla",
"houseNbr": "0014"
}],
"involvedObjects": []
}
],
"infos": []
}

 

 

I want to fetch the first and last name of role = viewer.  Unfortunately I cannot use and index since I don't know how many data tags will be delivered in response.

 

So I need to find a way to fetch a subset of the data (role = viewer) and than fetch the firstname and lastname which is part of the individual tag.

 

I hope someone can help me with fetching this data.

 

Thanks in advance!

  • Have a script with following snippet:

     

    //assign the response for jsonString Variable
    def jsonString = 
    
    def individualViewer = new groovy.json.JsonSlurper().parseText(jsonString).result.find{it.role == 'VIEWER'}.individual
    log.info "first name: ${individualViewer.firstName}"
    log.info "last name: ${individualViewer.lastName}"

     

    You can test it online

    https://ideone.com/yJSNYZ

  • nmrao's avatar
    nmrao
    Champion Level 3

    Have a script with following snippet:

     

    //assign the response for jsonString Variable
    def jsonString = 
    
    def individualViewer = new groovy.json.JsonSlurper().parseText(jsonString).result.find{it.role == 'VIEWER'}.individual
    log.info "first name: ${individualViewer.firstName}"
    log.info "last name: ${individualViewer.lastName}"

     

    You can test it online

    https://ideone.com/yJSNYZ

    • tsjakamaka's avatar
      tsjakamaka
      Occasional Contributor
      nmrao  Thanks for the quick (and correct) response!  You are my hero of the day!