ryan sheppard
software engineer
I wanted to share an easy way to make JSON output from the command line more readable. I’ve been using this for a while now and it’s been a huge help. This is for macOS, but I’m sure there are similar tools for other operating systems.
The Problem
I often copy the JSON message from our logging system and paste it into slack or a text editor. The problem is that the JSON is all on one line and it’s hard to read. Here’s an example:
{"level":"info","ts":1631494759.0000002,"caller":"log/log.go:33","msg":"Hello World","foo":"bar","bar":"baz"}
The Solution
With the combination of three commands we can make the JSON output much more readable. The three commands are pbpaste, pbcopy, and json_pp. All three commands are available on macOS by default.
pbpaste | json_pp | pbcopy
The first command, pbpaste
, will paste the contents of the clipboard into the terminal. The second command, json_pp
, will format the JSON output. The third command, pbcopy
, will copy the output back to the clipboard.
The Result
Here’s the same JSON message after running the commands above:
{
"level" : "info",
"ts" : 1631494759.0000002,
"caller" : "log/log.go:33",
"msg" : "Hello World",
"foo" : "bar",
"bar" : "baz"
}
Another Solution
You could also the excellent jq tool to format the JSON output. Here’s an example:
pbpaste | jq . | pbcopy