jsongroovyjsonslurper

Groovy: JSON Parsing


Seeing an interesting issue, not sure this is to do with parser or the way it suppose to parse. Any help is appreciated

import groovy.json.JsonSlurper

def dMatch = '''[{"match":{"keyId":"A-102161-application"}},{"match":{"keyId":"A-102162-application"}},{"match":{"keyId":"A-102163-application"}},{"match":{"keyId":"A-102164-application"}},{"match":{"keyId":"A-102165-application"}}]'''

println "T1:: List: " + dMatch

def parser = new JsonSlurper()
def exclude = parser.parseText(dMatch)
println "T2:: Obj: " + exclude.toString()

println "----------------------------------------------------"

Output :

T1:: List: [{"match":{"keyId":"A-102161-application"}},
            {"match":{"keyId":"A-102162-application"}},
            {"match":{"keyId":"A-102163-application"}},
            {"match":{"keyId":"A-102164-application"}},
            {"match":{"keyId":"A-102165-application"}}]
T2:: Obj: *[[match:[keyId:A-102161-application]], 
            [match:[keyId:A-102162-application]],  
            [match:[keyId:A-102163-application]],  
            [match:[keyId:A-102164-application]],  
            [match:[keyId:A-102165-application]]]*

The parsed object supposed to be same as the string but all the values were converted as array list of map.

Any idea why this is generating object like this ? When this is sent to camunda it complains

org.camunda.bpm.engine.ProcessEngineException: Cannot serialize object in variable 'exclude': groovy.json.internal.LazyMap


Solution

  • Use JsonSlurperClassic() - it produces standard HashMap that is serializable.

    And if you want to convert object back to json use Json output.toJson(obj)