Api

Using Python to Format Json

I have been working quite a bit with Json. Json disregards whitespace by implementation this makes the standard format of Json being a single very long line of not so readable text.
Simple example.
{"query":"some text","results":[{"title":"some result","id":"213"}]}
I had some plan to write some simple python script to format it nicely. I have used the json module before, I wasn’t aware it had a format function as part ofjson.tool.

curl + Json + Python
curl http://domain.com/api.json 2> /dev/null | python -mjson.tool

I found that with our redirection the error output to /dev/null i was getting.

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 67 0 67 0 0 76 0 –:–:– –:–:– –:–:– 0

Formated Json

{
    "query": "some text", 
    "results": [
        {
            "id": "213", 
            "title": "some result"
        }
    ]
}

Tags: , ,

Tuesday, July 14th, 2009 Programing Comments Off on Using Python to Format Json