Groovy web console

subscribe to the feed Subscribe
to this
site
Passing Base64'd JSON over URL (via #groovywebconsole)
tweet this snippet Tweet
this
script

Passing Base64'd JSON over URL

Published 3 months ago by hugheba@gmail.com with tags json
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.*
import java.net.*

// Print JSON Pretty
def jsonPretty = URLEncoder.encode(JsonOutput.prettyPrint(JsonOutput.toJson([name: 'John Doe', age: 42])).bytes.encodeBase64().toString(), "UTF-8") 
println jsonPretty

// Print Decoded JSON Pretty
def jsonDecode = new JsonSlurper().parseText(new String(URLDecoder.decode(jsonPretty, 'UTF-8').decodeBase64()))
println jsonDecode