0

I am trying to extract two sets of information from the httpResponse (in the form of JSON)-
1. Location
2. city where fruit = Apple and luckyNumber = 10.

{
    "userInformation": {
        "Name": "John",
        "Location": "India"
    },
    "details": [
        {
            "fruit": "Apple",
            "color": "Red",
            "city": "New Delhi",
            "luckyNumber": 10
        },
        {
            "fruit": "Banana",
            "color": "yellow",
            "city": "Goa",
            "luckyNumber": 12
         }
         ]
         }

For extracting the Location, I tried the below code:

def slurper = new JsonSlurper().parseText(httpResponse)

userLocation = slurper.userInformation.Location

This gives me an error -

javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.LinkedHashMap) values: [[statusCode:200, reason:OK, headers:[Access-Control-Allow-Credential:true, ...], ...]] Possible solutions: parseText(java.lang.String), parse([B), parse([C), parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader) 
ComplexData
  • 1,091
  • 4
  • 19
  • 36

2 Answers2

0

the error

No signature of method: groovy.json.JsonSlurper.parseText() is applicable for
       argument types: (java.util.LinkedHashMap)
Possible solutions: parseText(java.lang.String), ...

means that you try to pass Map (httpResponse) into JsonSlurper.parseText() when this method accepts String.

Find how to get response body as a string and then you can use JsonSlurper.parseText()

daggett
  • 26,404
  • 3
  • 40
  • 56
  • Can you help me how can I convert response body to string? I tried below code, but doesn't work: def jsonBody = new JsonBuilder(httpResponse).toString() def slurper = new JsonSlurper().parseText(jsonBody) – ComplexData Oct 10 '18 at 16:34
  • There's no need for to `String` conversion. You already have a `Map` and can work with it. – Opal Oct 10 '18 at 17:24
  • @ComplexData, could you show the code you use to get http response. – daggett Oct 10 '18 at 20:45
0

Probably you need httpResponse.getData() or just httpResponse.data to access response data payload. This data may already be in map, if response was parsed correctly based on Content-Type, in this case you do not need to use JsonSlurper. If data is a json String then use JsonSlurper.

In any case you will have something like

def cities = responseData.details.findAll{it.fruit=="Apple" && it.luckyNumber==10}
yeugeniuss
  • 180
  • 1
  • 7