How to roundtrip JSON like XML? (XmlSlurper -> GPathResult -> MarkupBuilder)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to roundtrip JSON like XML? (XmlSlurper -> GPathResult -> MarkupBuilder)

spex66
Hi experts,

I managed to find the following XML roundtrip:
1) x = new XmlSlurper().parse(fileHandle) // slurp
2) out = new StreamingMarkupBuilder()  // builder
3) String xmlResult = out.bind{mkp.yield x}

Now I'm looking for the dualism in JSON handling (Slurper -> Builder)
Expected output is to read one format, manipulate it and to write it back to XML or to JSON.

First step: Roundtrip JSON?
Second step: Cross XML/JSON and vice versa.

Is it easy to achieve?!?
thank you for any pointer

regards
Peter
(=PA=)


(=PA=)
.oO Started with Groovy1.8 just after 12 yrs of Python Oo.
Reply | Threaded
Open this post in threaded view
|

Re: How to roundtrip JSON like XML? (XmlSlurper -> GPathResult -> MarkupBuilder)

Guillaume Laforge-4
Hi Peter,

JsonSlurper returns a combination of lists and maps, representing respectively JSON arrays and JSON objects.
Lists and maps are very easy to manipulate from Groovy.
And then, JsonBuilder takes a list or a map in its constructor, so just feed it with your modified list or map, and voila.

Here's an example.

import groovy.json.*

// retrieve the commit messages for the Gaelyk project on GitHub
// parse that JSON document, select only the commit messages and turn them into uppercase
def msgs = new JsonSlurper().parseText(commits).commits.message*.toUpperCase()
// msgs now contains all the commit messages in uppercase
// now feed the json builder with that new structure, call toString() on it to get the new JSON, and use the pretty printer for a nicer output
println JsonOutput.prettyPrint(new JsonBuilder(msgs).toString())

Guillaume

On Fri, Jun 3, 2011 at 16:09, spex66 <[hidden email]> wrote:
Hi experts,

I managed to find the following XML roundtrip:
1) x = new XmlSlurper().parse(fileHandle) // slurp
2) out = new StreamingMarkupBuilder()  // builder
3) String xmlResult = out.bind{mkp.yield x}

Now I'm looking for the dualism in JSON handling (Slurper -> Builder)
Expected output is to read one format, manipulate it and to write it back to
XML or to JSON.

First step: Roundtrip JSON?
Second step: Cross XML/JSON and vice versa.

Is it easy to achieve?!?
thank you for any pointer

regards
Peter
(=PA=)




-----
(=PA=)
.oO Just started Groovy after 12 yrs of Python Oo.
--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-roundtrip-JSON-like-XML-XmlSlurper-GPathResult-MarkupBuilder-tp4451298p4451298.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one