I did find a solution while I was writing the question: don't put the input inside string interpolation, output a stream of things:
echo '{"foo":"bar", "baz":[1,2,3]}' | jq -r '"My value is:", . , "Some other stuff"'
# .........................................................^^^^^
outputs
My value is:
{
"foo": "bar",
"baz": [
1,
2,
3
]
}
Some other stuff
Answer from glenn jackman on Stack OverflowI did find a solution while I was writing the question: don't put the input inside string interpolation, output a stream of things:
echo '{"foo":"bar", "baz":[1,2,3]}' | jq -r '"My value is:", . , "Some other stuff"'
# .........................................................^^^^^
outputs
My value is:
{
"foo": "bar",
"baz": [
1,
2,
3
]
}
Some other stuff
This might not fit your actual use case, but instead of creating a single JSON string in jq, just use a shell command group.
echo '{"foo":"bar", "baz":[1,2,3]}' | { echo "My value is:"; jq .; echo "Some other stuff"; }
How to pretty print a JSON inside a string with jq? - Stack Overflow
No pretty print by default on JSON, compared to jq and python yq
request: make jq stdin accept json and output pretty print json
unix - How can I pretty-print JSON in a shell script? - Stack Overflow
Videos
This:
echo '{"foo": "bar"}' | jq '{other: .}' | jq -Rs '{json: .}'
produces:
{
"json": "{\n \"other\": {\n \"foo\": \"bar\"\n }\n}\n"
}
One way to remove the terminating "\n" would be to strip it:
echo '{"foo": "bar"}' | jq '{other: .}' | jq -Rs '{json: .[:-1]}'
I ended up writing a simple formatting function:
# 9 = \t
# 10 = \n
# 13 = \r
# 32 = (space)
# 34 = "
# 44 = ,
# 58 = :
# 91 = [
# 92 = \
# 93 = ]
# 123 = {
# 125 = }
def pretty:
explode | reduce .[] as $char (
{out: [], indent: [], string: false, escape: false};
if .string == true then
.out += [$char]
| if $char == 34 and .escape == false then .string = false else . end
| if $char == 92 and .escape == false then .escape = true else .escape = false end
elif $char == 91 or $char == 123 then
.indent += [32, 32] | .out += [$char, 10] + .indent
elif $char == 93 or $char == 125 then
.indent = .indent[2:] | .out += [10] + .indent + [$char]
elif $char == 34 then
.out += [$char] | .string = true
elif $char == 58 then
.out += [$char, 32]
elif $char == 44 then
.out += [$char, 10] + .indent
elif $char == 9 or $char == 10 or $char == 13 or $char == 32 then
.
else
.out += [$char]
end
) | .out | implode;
It adds unnecessary empty lines inside empty objects and arrays, but it's good enough for my purpose. For example (used on its own):
jq -Rr 'include "pretty"; pretty' test.json
where the function is saved in pretty.jq and test.json file is:
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"key":"string with \"quotes\" and \\"},"geometry":{"type":"Polygon","coordinates":[[[24.2578125,55.178867663281984],[22.67578125,50.958426723359935],[28.125,50.62507306341435],[30.322265625000004,53.80065082633023],[24.2578125,55.178867663281984]]]}}]}
gives:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"key": "string with \"quotes\" and \\"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
24.2578125,
55.178867663281984
],
[
22.67578125,
50.958426723359935
],
[
28.125,
50.62507306341435
],
[
30.322265625000004,
53.80065082633023
],
[
24.2578125,
55.178867663281984
]
]
]
}
}
]
}
With Python 2.6+ or 3 you can use the json.tool module:
echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
or, if the JSON is in a file, you can do:
python -m json.tool my_json.json
if the JSON is from an internet source such as an API, you can use
curl http://my_url/ | python -m json.tool
For convenience in all of these cases you can make an alias:
alias prettyjson='python -m json.tool'
For even more convenience with a bit more typing to get it ready:
prettyjson_s() {
echo "$1" | python -m json.tool
}
prettyjson_f() {
python -m json.tool "$1"
}
prettyjson_w() {
curl "$1" | python -m json.tool
}
for all the above cases. You can put this in .bashrc and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'.
Note that as @pnd pointed out in the comments below, in Python 3.5+ the JSON object is no longer sorted by default. To sort, add the --sort-keys flag to the end. I.e. ... | python -m json.tool --sort-keys.
Another useful option might be --no-ensure-ascii which disables escaping of non-ASCII characters (new in version 3.9).
You can use: jq
It's very simple to use and it works great! It can handle very large JSON structures, including streams. You can find their tutorials here.
Usage examples:
$ jq --color-output . file1.json file1.json | less -R
$ command_with_json_output | jq .
$ jq # stdin/"interactive" mode, just enter some JSON
$ jq <<< '{ "foo": "lorem", "bar": "ipsum" }'
{
"bar": "ipsum",
"foo": "lorem"
}
Or use jq with identity filter:
$ jq '.foo' <<< '{ "foo": "lorem", "bar": "ipsum" }'
"lorem"