0

I’m facing a problem, where I can’t parse response body content.

Here is what I use for parsing, that works for another responses but for current response it doesn’t work.

String getContent = get_response.getResponseBodyContent()
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(getContent)

And it gives me a following error: enter image description here

brithwulf
  • 538
  • 10
  • 35

1 Answers1

2

This is because you have a JSON array in your response body content. Try this one:

List parsedJson = slurper.parseText(getContent)

or just

def parsedJson = slurper.parseText(getContent)

Detailed example:

def json = """
[
    {
        "companyName":"Foo",
        "customerId":"Bar"
    },
    {
        "companyName":"Foo2",
        "customerId":"Bar2"
    }
]
"""
def slurper = new JsonSlurper()
//Map mapJson = slurper.parseText(json) FAIL!!!
List listJson = slurper.parseText(json)
def objJson = slurper.parseText(json)

objJson.each { map ->
    println(map)
}

Output:

[companyName:Foo, customerId:Bar]
[companyName:Foo2, customerId:Bar2]
Dmitry Khamitov
  • 3,061
  • 13
  • 21
  • Now, I getting another error. org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.get() is applicable for argument types: (java.lang.String) values: [id] Possible solutions: get(int), get(int), getAt(java.lang.String), getAt(java.lang.String), grep(), grep() – brithwulf Jan 30 '19 at 12:55
  • 1
    Probably it's because you are trying to access a value by string key in `parsedJson` which is a list. If this is the case try this `parsedJson[0]["companyName"]`. This way you will access `companyName` of the first element in the parsed json – Dmitry Khamitov Jan 30 '19 at 13:00
  • Thanks, that works well ! But why does not work Map for my case ? – brithwulf Jan 30 '19 at 13:09
  • 1
    You can see on your screenshot that you have a JSON array surrounded by square brackets `[{...}]`. More info can be found at [JsonSlurper doc](http://groovy-lang.org/json.html#json_jsonslurper). Also, if you declare a variable type Groovy tries to cast the right hand side expression to that type – Dmitry Khamitov Jan 30 '19 at 13:14
  • @Dmitry, I am trying the similar one with the proposed solution. However, I am unable to filter the content and print it out. Any suggestions, please ? – user1911509 Feb 26 '19 at 20:23
  • my json : [ { "Name": "Apol1", "Title": "Apol1", "Domain": "apollo1.com", "DCs": [ "Email add", "hints" ], "IsVerified": true, "IsActive": false }, { "Name": "Apol2", "Title": "Apol2", "Domain": "apollo2.io", "DCs": [ "Email add", "Phone" ], "IsVerified": true, "IsActive": false }, { "Name": "Apollo3", "Title": "Apollo3", "Domain": "apollo3.com", "DCs": [ "Email add", "Pws" ], "IsVerified": true, "IsActive": false } ] – user1911509 Feb 26 '19 at 20:24
  • Any ideas, how can I get all the names and all the DCs ? – user1911509 Feb 26 '19 at 20:56
  • @user1911509 If I got you right, you can retain `Name` and `DCs` in the resulted `List` of `Map`. Try this `objJson.each { map -> map.retainAll { k, v -> k in ['Name', 'DCs'] } }`. If you want to print the result you can either print as you go after `retainAll` statement or in a dedicated `objJson.each { map -> println(map) }` after the aforementioned `objJson.each {...}` – Dmitry Khamitov Feb 27 '19 at 08:46