Installing simplejson on Python 2.4 for CentOS 5.7

I tried to install a few different JSON printers on a CentOS 5.7 box I was trying to use, but nothing was compiling. I checked Stack Overflow and tried a few of the python suggestions, but none of those worked either. I realised that it’s because I’m running Python 2.4, and that has no JSON tools built in. Once I downloaded and installed simplejson everything worked fine.

Installing simplejson 2.0.9

wget http://pypi.python.org/packages/source/s/simplejson/simplejson-2.0.9.tar.gz#md5=af5e67a39ca3408563411d357e6d5e47
tar xzvf simplejson-2.0.9.tar.gz
cd simplejson-2.0.9
sudo python setup.py install

Creating prettyjson

We want to be able to pipe content into our pretty printer easily. To do this, create a new file named prettyjson somewhere in your $PATH and save the following content into it:

#!/usr/bin/env python
"""
Convert JSON data to human-readable form.

(Reads from stdin and writes to stdout)
"""

import sys
import simplejson as json


print json.dumps(json.loads(sys.stdin.read()), indent=4)
sys.exit(0)

Once you’ve saved it, run chmod +x /path/to/prettyjson and you’re ready to start using it

Usage

echo '{"a":"b"}' | prettyjson

You can pipe any input into it, e.g:

script_that_outputs_json | prettyjson

Michael is a polyglot software engineer, committed to reducing complexity in systems and making them more predictable. Working with a variety of languages and tools, he shares his technical expertise to audiences all around the world at user groups and conferences. You can follow @mheap on Twitter

Thoughts on this post

BEN 2017-04-03

Worked like charm..!

Leave a comment?

Leave a Reply