JSON command line tools
JSON is the lingua franca1 of modern software development. It is not ideal, but much better than alternatives.
XML is too verbose, ambiguous and hard to write by hand. YAML is spoiled with unobvious defaults and has unfriendly nested structures syntax.
JSON, on the contrary, is natively supported in browsers: parsing it could be much faster than parsing a text file.
I’d like to share two invaluable command-line tools to work with JSON that I use daily.
jq
jq
filters and transforms JSON data according to the provided template.
It is proven by time and is available in any Linux distro or in Homebrew (brew install jq
).
For example, here is how one can extract all descriptions from an array of entries:
cat feed.json | jq ".entries[]?.description"
or process an HTTP response:
CROWDIN_BUILD=`curl --silent --header "Authorization: Bearer $CROWDIN_TOKEN" https://crowdin.com/api/v2/projects/$CROWDIN_PROJECT_ID/translations/builds | jq '.data[0].data.id'`
TRANSLATIONS_URL=`curl --silent --header "Authorization: Bearer $CROWDIN_TOKEN" https://crowdin.com/api/v2/projects/$CROWDIN_PROJECT_ID/translations/builds/$CROWDIN_BUILD/download | jq --raw-output '.data.url'`
curl --silent $TRANSLATIONS_URL --output $builddir/translations.zip
This is a real snippet I wrote recently to download translations from Crowdin.
I can’t imagine automating build scripts without jq
.
Learn more in the tutorial and the official documentation.
jq
makes it so easy to read and modify JSON that some people prefer to convert plain text to JSON
to analyze it with tools like jc.
jless
jless
is a command-line JSON viewer. It is interactive,
so I use it for quick look-ups and for investigation of unknown JSON responses.
Here is the demo worth a thousand words.
And after I learned everything I wanted from the file, I can automate it with jq
.
And what tools do you use to process JSON? Any recommendations?
Subscribe to all blog posts via RSS