I\'ve a file with a sequence of JSON element:
{ element0: \"lorem\", value0: \"ipsum\" }
{ element1: \"lorem\", value0: \"ipsum\" }
...
{ elementN: \"lorem\"
In the Mac OS, install jq
with the command,
$ brew install jq
You can get the pretty print JSON as similar as,
$ curl -X GET http://localhost:8080/api/v1/appointments/1 | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 117 0 117 0 0 8404 0 --:--:-- --:--:-- --:--:-- 9000
{
"craeted_at": "10:24:38",
"appointment_date": "2019-02-08",
"name_of_doctor": "Monika",
"status": true,
"price": 12.5,
"id": 1
}
Shawn's solution but for Python 3:
echo '{"foo": "bar"}' | python3 -m json.tool
I felt this deserved its own entry when it took me longer than it should have to discover. I was looking for a simple way to pretty-print the json output of docker inspect -f
. It was mentioned briefly above by Noufal Ibrahim as part of another answer.
From the jq website (https://stedolan.github.io/jq/):
jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.
It provides colored output by default and you simply have to pipe to jq
, e.g.
cat file | jq .
Example:
"Raw" json output vs the same piped to jq
You can use jtab - a tool written in rust - to print any json data as a table.
For example:
➜ echo '{"foo": "bar"}' | jtab
+-----+
| foo |
+-----+
| bar |
+-----+
It also works with a json array:
➜ echo '[{"id": "1", "name": "Rust"}, {"id": "2", "name": "Jtab"}]' | jtab
+----+------+
| id | name |
+----+------+
| 1 | Rust |
+----+------+
| 2 | Jtab |
+----+------+
You can use Python JSON tool (requires Python 2.6+).
For example:
echo '{ "element0" : "lorem", "element1" : "ipsum" }' | python -m json.tool
Which will give you:
{
"element0": "lorem",
"element1": "ipsum"
}
with python (2 and 3):
alias prettify_json="python -c 'import sys ;import json ; print(json.dumps(json.loads(sys.stdin.read()), indent=4))'"
or with ruby:
alias prettify_json="ruby -e \"require 'json';puts JSON.pretty_generate(JSON.parse(STDIN.read))\""
you can use:
echo '{"bar": "abc", "foo": "def"}' | prettify_json
curl http://.../file.json | prettify_json