E.T.
E.T.

Reputation: 189

JsonSlurper parsing String containing Json into unexpected format

From a separate system I get a String parameter "messageJson" whose content is in the form:

{"agent1":"smith","agent2":"brown","agent3":{"agent3_1":"jones","agent3_2":"johnson"}}

To use it in my program I parse it with JsonSlurper.

def myJson = new JsonSlurper().parseText(messageJson)

But the resulting Json has the form:

[agent1:smith, agent2:brown, agent3:[agent3_1:jones, agent3_2:johnson]]

Note the square brackets and the lack of double quotes. How can I parse messageJson so that the original structure is kept?

Upvotes: 0

Views: 1206

Answers (1)

E.T.
E.T.

Reputation: 189

Ok, thanks to the hint by cfrick, I was able to find a solution. In case anyone else has a similar problem, all I needed to do was using JsonOutput in the end to convert the map back to a Json

I.E. :

def myJson = new JsonSlurper().parseText(messageJson)
myJson << [agent4:"jane"]
def backToJson = JsonOutput.toJson(myJson)

Upvotes: 2

Related Questions