jq - a command line JSON processor

I do lots of log processing when debugging issues or analyzing performance. But nowadays many logs are no longer in simple tabular format that can easily be parsed with grep and other standard command-line tools, but programmers increasingly store outputs of their programs in JSON format. Sometime ago Tomaž showed me a great little tool for processing streams of JSON data and this weekend I finally got some time to explore it in more depth. jq is a very powerful tool that enables a very complex processing of JSON data. For example, the command

[code language="bash"] jq 'reduce .[] as $item ({}; . + {($item.name):$item.picture})' [/code]

will take a sample address book and convert it into a dictionary with the following structure

[code language="javascript"] { "Allen Joyner": "http://placehold.it/32x32", "Lorna English": "http://placehold.it/32x32", "Cook Marquez": "http://placehold.it/32x32", "Alston Brooks": "http://placehold.it/32x32", "Corrine Hyde": "http://placehold.it/32x32", "Montoya Horn": "http://placehold.it/32x32" } [/code]

Doing such complex transformation would require quite some lines of Python code. I think it's well worth spending some time and learn jq syntax. It will be a great addition to your tool belt.