Forum Discussion

CharlesHarold's avatar
CharlesHarold
Occasional Contributor
7 years ago

How to Parse a JSON Response Using Groovy (Examples!)

This code can be copied directly into a Groovy Script test step and run to demonstrate a few ways to get data from a JSON REST response:

 

// Some Examples of How to Parse a JSON Response Using Groovy
// set the example json response string (for a REST Request step assertion, use "def json = messageExchange.response.contentAsString")
def json = '[{"firstName":"Bob","lastName":"Smith","uniqueId":146732,"thisIsAlwaysNull":null,"jobInfo":{"title":"","type":"Peon","code":42},"reviews":[{"date":"2017-06-01","type":"Regular","rating":"Adequate"},{"date":"2017-09-15","type":"Special","rating":"Other"}]},{"firstName":"Jack","lastName":"Jones","uniqueId":746381,"thisIsAlwaysNull":null,"jobInfo":{"title":"Big Boss","type":"Management","code":1},"reviews":[{"date":"2007-11-05","type":"Initial","rating":"Spectacular"}]},{"firstName":"Will","lastName":"Tell","uniqueId":574831,"thisIsAlwaysNull":null,"jobInfo":{"title":"Sweeper","type":"Peon","code":452},"reviews":[]}]'
// parse json string using JsonSlurper - basically converts it to a groovy list
def parsedJson = new groovy.json.JsonSlurper().parseText(json)
// get data
log.info " Count of people returned: " + parsedJson.size()
log.info " Was Will's info returned (exists)? " + ( parsedJson.find { it.firstName == "Will" } != null )
log.info " Was Alice's info NOT returned (not exists)? " + ( parsedJson.find { it.firstName == "Alice" } == null )
log.info " First person's first name: " + parsedJson[0].firstName
log.info " Index of person with ID 746381: " + parsedJson.findIndexOf { it.uniqueId == 746381 }
log.info " Info for person with last name Tell: " + parsedJson.find { it.lastName == 'Tell' }
log.info " Jack's ID: " + ( parsedJson.find { it.firstName == 'Jack' } ).uniqueId
log.info " Jack Jones's job title: " + ( parsedJson.find { it.firstName == 'Jack' && it.lastName == 'Jones' } ).jobInfo.title
log.info " All peon job type people's first names: " + ( parsedJson.findAll { it.jobInfo.type == 'Peon' } ).firstName
log.info " Is Will's thisIsAlwaysNull null? " + ( ( parsedJson.find { it.firstName == 'Will' } ).thisIsAlwaysNull == null )
log.info " Will Tell's had this many reviews: " + ( parsedJson.find { it.firstName == 'Will' && it.lastName == 'Tell' } ).reviews.size()
log.info " Bob's 2017-06-01 review rating: " + ( ( parsedJson.find { it.firstName == 'Bob' } ).reviews.find { it.date == '2017-06-01' } ).rating

Hope this helps someone else!

  • CharlesHarold's avatar
    CharlesHarold
    Occasional Contributor

    I should point out that (most of) the parentheses and spaces are not necessary; the following will work the same:

    log.info " Count of people returned: " + parsedJson.size()
    log.info " Was Will's info returned (exists)? " + ( parsedJson.find{it.firstName=='Will'} != null )
    log.info " Was Alice's info NOT returned (not exists)? " + ( parsedJson.find{it.firstName=='Alice'} == null )
    log.info " First person's first name: " + parsedJson[0].firstName
    log.info " Index of person with ID 746381: " + parsedJson.findIndexOf{it.uniqueId==746381}
    log.info " Info for person with last name Tell: " + parsedJson.find{it.lastName=='Tell'}
    log.info " Jack's ID: " + parsedJson.find{it.firstName=='Jack'}.uniqueId
    log.info " Jack Jones's job title: " + parsedJson.find{it.firstName=='Jack'&&it.lastName=='Jones'}.jobInfo.title
    log.info " All peon job type people's first names: " + parsedJson.findAll{it.jobInfo.type=='Peon'}.firstName
    log.info " Is Will's thisIsAlwaysNull null? " + ( parsedJson.find{it.firstName=='Will'}.thisIsAlwaysNull == null )
    log.info " Will Tell's had this many reviews: " + parsedJson.find{it.firstName=='Will'&&it.lastName=='Tell'}.reviews.size()
    log.info " Bob's 2017-06-01 review rating: " + parsedJson.find{it.firstName=='Bob'}.reviews.find{it.date=='2017-06-01'}.rating

    And if you want to dynamically build the code to get a specific value as a string, and then evaluate it as code, the following works:

    def codeSnippet = "parsedJson.find{it.firstName=='Jack'&&it.lastName=='Jones'}.jobInfo.title"
    log.info " Evaluate string as code: " + Eval.me( "parsedJson", parsedJson, codeSnippet )

     

    • sprice090161's avatar
      sprice090161
      Contributor

      Thank you for the example. I have a json response containing multiple objects and I need to determine the index of the object that I want. "findIndexOf" will be quite helpful.

       

      Thank You

       

      Steve Price

  • lhimo's avatar
    lhimo
    New Contributor

    Thanks for the suggestion. I found though that if you have an array of elements the .findResult function works only for the first element, isn't it? The .find method instead seems to work properly.