Wednesday, August 13, 2014

Groovy Notes: Pretty print JSON from the command line

JSON pretty print it's common task while working with JSON from command line. There are many ways of doing this via Python, Ruby, node.js, but here I'd like to concentrate on Groovy one-liner:

$ echo '{"foo": "lorem", "bar": "ipsum"}' | groovy -e 'import groovy.json.*; println JsonOutput.prettyPrint('

    "foo": "lorem",
    "bar": "ipsum"

We can slightly improve this one-liner via adding shell alias:
$ alias pp="groovy -e 'import groovy.json.*; println JsonOutput.prettyPrint('"
$ echo '{"foo": "lorem", "bar": "ipsum"}' | pp

Also, we might use Groovy script which might be handy for simple JSON validation also:

$ cat prettyJson.groovy 
import groovy.json.*

try {
  println JsonOutput.prettyPrint(
} catch (JsonException e) {
  println "ERROR: Not valid JSON"

$ echo '{"foo: "lorem", ' | groovy prettyJson.groovy
ERROR: Not valid JSON