wiki:use_jq_utility_pretty_format_json
This is an old revision of the document!
Table of Contents
Use jq utility to pretty format json
If you get an error like
parse error: Invalid numeric literal at line 1, column 7
this is could be because the json contains single quotes instead of double quotes. This is invalid JSON:
{'red': {'hue': {'dark': 2, 'white': 1}}, 'blue': {'blue': {'dark': 3, 'white': 3}, 'blue-2': {'dark': 3, 'white': 3}}}
JSON uses double quotes for strings. Use this to pipe the command to sed. Notice the double quotes around the whole json in echo command (it won't work otherwise):
echo "{'red': {'hue': {'dark': 2, 'white': 1}}, 'blue': {'blue': {'dark': 3, 'white': 3}, 'blue-2': {'dark': 3, 'white': 3}}}" | sed 's/'\''/"/g' | jq .
Output:
{
"red": {
"hue": {
"dark": 2,
"white": 1
}
},
"blue": {
"blue": {
"dark": 3,
"white": 3
},
"blue-2": {
"dark": 3,
"white": 3
}
}
}
Tested on
- Ubuntu 20.04.4 LTS
See also
References
wiki/use_jq_utility_pretty_format_json.1654594021.txt.gz · Last modified: by antisa
