2

I am trying to fetch the first node value (ResourceItemID i.e 2290) from my JSON response. My Response is look like :

    {
   "Success": true,
   "TotalRecords": 41,
   "RoomSearchResult":    [
            {
         "ResourceItemID": 2290,
         "Name": "Room 23 (L02)",
         "LocationId": 7,
         "GroupID": 518,
         "FloorID": 2,
         "DefaultCapacity": 4,
         "CanBeBooked": true
      },
{
         "ResourceItemID": 2063,
         "Name": "Room 15 (L10)",
         "LocationId": 7,
         "GroupID": 518,
         "FloorID": 10,
         "DefaultCapacity": 8,
         "CanBeBooked": true
      }
   ],
   "Error":    {
      "ErrorCode": 0,
      "ErrorDescription": ""
   }
}

What i tried so far :

import groovy.json.JsonSlurper
def parsed = new JsonSlurper().parseText(json).find().value.RoomSearchResult.ResourceItemID
5
  • Do you need to extract ResourceItemID when condition is matched. Commented Jun 12, 2017 at 3:47
  • No, ResourceItemID can be anything. I just need to find and store first value into soapui property. Commented Jun 12, 2017 at 4:59
  • Ok, but there are list of values. Is it that 2290 a fixed value that you are looking for? Commented Jun 12, 2017 at 5:03
  • No, This value changes depends on my request. I just need to find first value whatever it is. OR may be sometimes need a second/third/Fourth... value. Commented Jun 12, 2017 at 5:22
  • Ok. Then how do you decide which value to pick from the list? Commented Jun 12, 2017 at 7:05

2 Answers 2

3

If you want only the first node value then you don't need to traverse the whole JSON manually, you can just parse it to collection and get the first node from it.

import groovy.json.JsonSlurper


String jsonString = """
  {
   "Success": true,
   "TotalRecords": 41,
   "RoomSearchResult":    [
            {
         "ResourceItemID": 2290,
         "Name": "Room 23 (L02)",
         "LocationId": 7,
         "GroupID": 518,
         "FloorID": 2,
         "DefaultCapacity": 4,
         "CanBeBooked": true
      },
      {
         "ResourceItemID": 2063,
         "Name": "Room 15 (L10)",
         "LocationId": 7,
         "GroupID": 518,
         "FloorID": 10,
         "DefaultCapacity": 8,
         "CanBeBooked": true
      }
   ],
   "Error":    {
      "ErrorCode": 0,
      "ErrorDescription": ""
   }
}
"""

JsonSlurper jsonSlurper = new JsonSlurper()

/**
* 'jsonString' is the input json you have shown
* parse it and store it in collection
*/
Map convertedJSONMap  = jsonSlurper.parseText(jsonString)

//If you have the nodes then fetch the first one only
if(convertedJSONMap."RoomSearchResult"){

    println "ResourceItemID : " + convertedJSONMap."RoomSearchResult"[0]."ResourceItemID"
}   

Output:

ResourceItemID : 2290
Sign up to request clarification or add additional context in comments.

9 Comments

Error :- No such property: jsonString for class: Script7
Please have a look at updated answer. Input string is now declared above for better clarity.
Thanks Prakash, But my response is not always static. It keeps on changing according to my request. Is this possible to pass dynamic response in 'jsonString'.
Yes. I have taken it static for just showing the working example. You can use your dynamic json as input to JSON Slurper. But your json will always contain RoomSearchResult right ?
Then above solution will work for you. If it works as you expect, please accept and upvote the answer so that others will also get it.
|
0

You first need to navigate the nodes to get to the specific node type you want to extract the value from, not value first. Navigating down to the ResourceItemIDand then getting the value will return an array of the values of all ResourceItemIDs: [2290,2063]. You can either get the first item of the array or find based on the value itself. The code below will print the example results:

[2290, 2063] 2290 2290

def parsed = new JsonSlurper().parseText(json).RoomSearchResult.ResourceItemID.value
println parsed
def result = parsed.find{ value -> value == 2290}
println result
result = parsed[0]
println result

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.