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.

Rao
  • 20,781
  • 11
  • 57
  • 77
avidCoder
  • 440
  • 2
  • 10
  • 28

3 Answers3

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'}
Rao
  • 20,781
  • 11
  • 57
  • 77
  • 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 – avidCoder Dec 29 '17 at 07:44
  • 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. – Rao Dec 29 '17 at 07:45
  • 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. – avidCoder Dec 29 '17 at 07:50
  • 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](https://stackoverflow.com/tour) – Rao Dec 29 '17 at 07:54
1
Map m = new groovy.json.JsonSlurper().parseText(json)
m.book.each{println it}

This is enough.

dev-eloper
  • 110
  • 11
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]    

}
avidCoder
  • 440
  • 2
  • 10
  • 28