Search code examples
groovyjsonslurper

How can I stop JsonSlurper from converting objects/maps to arrays?


I have a JSON response item from a web service that looks like this:

[
    {
        "field1":"value",
        "field2":"value2",
        "field3":"value3",
        "field4":"value4"
    },
    {
        "field1":"value",
        "field2":"value2",
        "field3":"value3",
        "field4":"value4"
    },
    ...
]

Before conversion my response string looks normal (like this):

[{"field1":"value","field2":"value2","field3":"value3", "field4":"value4"},{...},...]

However after I run def allData = new JsonSlurper().parseText(response)

and then log allData it appears that it's converting my objects to arrays:

Example:

[["field1":"value","field2":"value2","field3":"value3", "field4":"value4"],[...],...]

Does anyone know why this is happening?

Edit:

Imports: import groovy.json.JsonSlurper


Solution

  • To clarify the (admittedly long) comments above for anyone who has this problem in the future:

    That is an array of maps. The confusion came from a difference in String representation between JSON and Groovy, but the underlying data structure was already correct.