Reputation: 247
Values are present in response, can print them via log.info but giving me an error when adding them in array, here is my groovy script,
import groovy.json.*
def ResponseMessage = ''' {
"Unit": {
"Profile": 12,
"Name": "Geeta"
},
"UnitID": 2
} '''
def json = new JsonSlurper().parseText(ResponseMessage)
log.info json.UnitID
log.info json.Unit.Profile
log.info json.Unit.Name
def arrayjson = json.collectMany { s ->
[s.UnitID,s.Unit.Profile,s.Unit.Name]
}
log.info "arrayjson : " + arrayjson
And The error message ,
groovy.lang.MissingPropertyException: No such property: UnitID for class: java.util.HashMap$Entry Possible solutions: key error at line: 14
Upvotes: 0
Views: 9781
Reputation: 24468
The collectMany
iterates over key/value pairs. Consider the following (as far as I understand the goal):
import groovy.json.*
def ResponseMessage = ''' {
"Unit": {
"Profile": 12,
"Name": "Geeta"
},
"UnitID": 2
} '''
def json = new JsonSlurper().parseText(ResponseMessage)
println json.UnitID
println json.Unit.Profile
println json.Unit.Name
// this illustrates how collectMany works, though it does
// not solve the original goal
json.collectMany { key, val ->
println "key: ${key} , val: ${val}"
[]
}
def arrayjson = [json.UnitID,json.Unit.Profile,json.Unit.Name]
println "arrayjson : " + arrayjson
Upvotes: 2