Category Archives: SysAdmin

Inline parsing JSON objects in 3 languages

JavaScript Object Notation commonly known as JSON, is a convenient format for many reasons and it is no wonder that many APIs and web services today support returning (in some cases exclusively) JSON.  Many times I have had a need to parse values from large JSON Objects in a shell script environment or a command line shell, and use them as inputs to another program.  This has put me on the look out for easy ways to handle inline parsing of JSON Objects using languages commonly available on modern Linux systems.

Below are examples in three languages (Node JS, Python and Perl) to accomplish this task.

Example JSON Object:

{"product": {"builds": {"1234": "IT WORKS!"}, "default": "1234"}}

Inline Node JS:

node -e "s=''; i=process.stdin; i.on('data', function(d) { s += d }); i.on('end', function() { j=JSON.parse(s).product; console.log(j.builds[j.default]) })"

Inline Python:

python -c 'import sys, json; j=json.load(sys.stdin)["product"]; print j["builds"][j["default"]]'

Inline Perl:

perl -e 'use JSON; local $/; $d=decode_json(<>)->{product}; print $d->{builds}->{$d->{default}}'
  • The JSON module is required in the above example. If not already available it can easily be installed via CPAN with:
sudo perl -MCPAN -e 'install JSON'


Full example for Python:

echo '{"product": {"builds": {"1234": "IT WORKS!"}, "default": "1234"}}' |python -c 'import sys, json; j=json.load(sys.stdin)["product"]; print j["builds"][j["default"]]'

3ware RAID controllers and the command prompt

3ware makes some great RAID products however when it comes down to configuring them things get a little confusing as their website isn’t completely clear what is offered in the packages available for download. Suffice to say that if you are looking to manage a 3ware RAID controller from a Linux command prompt then the little gem you’re looking for is called ‘tw_cli’ and NOT the 3dm2 package which is some sort of web interface frontend package; completely useless to shell guys like me.

If you are reading this and have already went down the 3dm2 road like I did, you can get rid of it by running “install.3dm uninstall” which isn’t that intuitive or obvious but works great! Just make sure you kill the 3dm2 processes first if they’re running.

Then head over to 3ware and get the goods: http://www.3ware.com/support/download.asp