Blog » Herbert Poul's Personal Blog » jsontidy - Pretty printing JSON data
I recently came across the problem of needing to "pretty print" a json output to debug a JSON API call. The only script i came across was a .NET application?!?! anyway, a few lines of python did the trick (i guess 1 line of perl would've done the job, but it doesn't have a standard JSON library)

#!/usr/bin/python

import sys
import json

x = json.load(sys.stdin)
print json.dumps(x, indent = 2)


pretty sophisticated, right? well... 10 minutes of googling solved by 1 minute of coding.. great

usage is obvious i guess...

Herbys-i7:~ herbert$ curl -s \
  "http://xxx/data/admin?cmd=listInactiveUsers&format=djson" \
  | jsontidy
{
  "users": [
    {
      "username": "test5", 
      "id": "d3292a0c-d152-42b1-98d0-335ded789bc6"
    }, 
    {
      "username": "test2", 
      "id": "0d947487-bccc-4742-bf4e-0b0afeb2011f"
    }
  ]
}


Of course you could also do it in one line.. how about using the following in your .bash_profile?

alias jsontidy='python -c "import sys,json;json.dump(json.load(sys.stdin), sys.stdout, indent=2)"'


Hey, we have Signatures !!! Great, isn't it ? ;)

Posted by Herbert Poul

Assigned Tags: development, debug, json

1 Comments
Comments:
or if you use firefox JSONView comes in handy too: https://addons.mozilla.org/en-US/firefox/addon/10869/

Please login to post a reply.

Personal website and blog of Herbert Poul. Also check out my Photo Gallery.




Herby's Photo Gallery

Subscriptions

User

You are not logged in.
Login
Register