3

I have the following JSON format :-

{
"id": "102",
"brand": "Disha",
"book": [{
    "slr": "EFTR",
    "description": "Grammer",
    "data": "TYR",
    "rate": true,
    "numberOfPages": 345,
    "maxAllowed": "12",
    "currentPage": 345
    },
    {
    "slr": "EFRE",
    "description": "English",
    "data": "TYR",
    "rate": true,
    "numberOfPages": 345,
    "maxAllowed": "12",
    "currentPage": 345
    },
    {
    "slr": "BGTR",
    "description": "French",
    "data": "TYR",
    "rate": true,
    "numberOfPages": 345,
    "maxAllowed": "12",
    "currentPage": 345
    }]
}

I want to write the groovy code to get the book array and print it one by one and before that I need to count the array node for book also.

I have tried below code:-

def response = context.expand( '${book#Response}' );
def slurper = new JsonSlurper();
String inputJSON = slurper.parseText(response)
def strFinalValueToRead = "\$." + "book[0]"
def strActualValue = parse(inputJSON).read(strFinalValueToRead).toString()

log.info strActualValue

I am getting error as

com.jayway.jsonpath.InvalidJsonException: net.minidev.json.parser.ParseException: Unexpected End Of File.

Any help would be appreciated.

3
  • The code works in my computer using the json provided by you. Commented Dec 29, 2017 at 6:53
  • I don't know, May be I am missing some external jar file for JSON. But I am not able to replicate this. Commented Dec 29, 2017 at 6:58
  • @Ashuans, see if my solution helps ? Commented Dec 29, 2017 at 7:43

3 Answers 3

1

You can use Script Assertion for the same REST Request test step which can avoid another additional groovy step.

Script Assertion

assert context.response, 'Response is empty or null'

def json = new groovy.json.JsonSlurper().parseText(context.response)
log.info json.book

The above logs all the book details.

You may also use index to show a particular book details such as show book at 0 index.

log.info json.book[0]

It is also find certain book based on some filter. For instance, find a book whose description is Grammer.

log.info json.book.find {it.description == 'Grammer'}
Sign up to request clarification or add additional context in comments.

4 Comments

Here, I am not confirm about the response I will be getting. The number of node for book or number of element in book array will be differing. So, In this case the above may not work. @Rao
Which part of the above does not work? Just showed a way to log 0th index. In order to display all the books, you do not need to write a for loop. Just look at the script assertion.
Oh., I mean if i'd have book array with undefined number of times which will be dynamic. And I want all of them to be printed.
Yes, log.info json.book does that. More over one less test step as Script Assertion is used with above approach. Appreciate upvotes for helpful answers and if you think best answer, consider accepting as answered
1
Map m = new groovy.json.JsonSlurper().parseText(json)
m.book.each{println it}

This is enough.

Comments

0

Finally, after so much effort. I got the solution and tried it with Map and List.

Object inputJSON = slurper.parseText(response)

def countBook = inputJSON.book.size()

for(int i=0; i<countBook; i++) {
Map result = (Map) inputJSON
List bookNode = result.get("book")
log.info bookNode[i]    

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.