# Parse a JSON Response Using Groovy

**URL:** <https://community.smartbear.com/t/parse-a-json-response-using-groovy/76>\
**Category:** API Articles\
**Tags:** readyapi, api, migrated, kb-article\
**Created:** [September 3, 2021, 1:28pm UTC](https://community.smartbear.com/t/parse-a-json-response-using-groovy/76 "2021-09-03T13:28:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [September 3, 2021, 1:28pm UTC](https://community.smartbear.com/t/parse-a-json-response-using-groovy/76/1 "2021-09-03T13:28:33Z")

</div>

> Originally published by **TanyaYatskovska** on September 03, 2021 in the previous SmartBear Community.

## Symptoms

Quite often we need to parse a JSON response to get some value or to compare the received value with the target one. 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

## Solution

```auto
// 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

```
