JSON and the shell. A love story?
JSON
and the shell haven’t been easy to deal with. I didn’t know of a JSON
parser (or builder) for the command line. Since a few days ago. Something magical happened 🧞♂️. I found jq. I don’t know where exactly. I think I was googling something about curl
and vault
. If you are a curl
user you know it’s not fun to work with the terminal and JSON
. Or curl
and JSON
.
But jq
changes all that. It’s freaking brilliant! You can build, you can parse, you can query, you can pipe it, and you can man
it. Nothing extraordinary, just ordinary shell stuff.
Build JSON
Use the option --null-input/-n
to build JSON:
$ jq -n '{foo: "bar"}'
{
"foo": "bar"
}
You can even be a bit lazy and skip the quotes 🙇♂️.
Interpolation
$ jq -n --arg bar "baz" '{foo: $bar}'
{
"foo": "baz"
}
File inclusion
$ jq -n --rawfile my_key ~/.ssh/id_rsa.pub '{key: $my_key}'
{
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2CorANrbmiswqLGpLqY56IU6+PfsONKWUVLDzKDYXmkndXzDvrEXDgTkeI8bKDAEcI0+Q2LgLbDhsAZCdwVnRWDwa3tDjI5oC20lFoTDWYQSZxmdLzocU/zOawj+Wh22Kk/IamYC4qdNlgAzLoM720IUBWh9b46CFs2So8kK4aDLhzShRp7s2UbdHlc7LPRpH80OIawYn3HgNw4U4kBKEM6VY8pGPfED9C19WkZ3lRQUOvRL6oAtopsG1QoICXNHPJPTBqSNCDudUrmkOhRmnL+nOBuAbmZQAIvMWXtILUlZGN+szuGKgZtKWQ73tZ0oFrGM/9ApuSSRu0HfIH56d [email protected]\n"
}
Parse JSON
Pretty print JSON from stdin:
$ echo '{"sub":"1234567890","name":"John Doe","iat":1516239022}' | jq .
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
Curl example
When you want to be able to read your curl responses:
$ curl -s 'http://echo.jsontest.com/hello/world' | jq .
{
"hello": "world"
}
The bad news is that it’s not included in any major operation system dist, at least not any I know of.