25 Feb '15, 12am

jq is sed for JSON

sed is a useful tool that reformats and transforms plain text. But sed is not a good match for structured data like JSON. jq is a sed-like tool that is specifically built to deal with JSON. Installation Install jq on OS X: brew install jq Or on Ubuntu: apt-get install jq Your packaging system probably has jq available. Usage jq is built around filters. The simplest filter is . , which echoes its input, but pretty-printed: $ echo '{"hello":{ "greetings":"to you"}}' | jq . { "hello": { "greetings": "to you" } } It’s useful for pretty-printing output from curl when you hit a JSON API, for example. As the well-written manual points out, the simplest useful filter is .field , which pulls field out of each record: $ echo '{"hello":{ "greetings":"to you"}}' | jq .hello { "greetings": "to you" } We can also do .field1.field2 for nested hashes: $ echo '{"hello":{ "greetings":"to yo...

Full article: https://robots.thoughtbot.com/jq-is-sed-for-json