В итоге я написал простую функцию форматирования:
# 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;
Она добавляет ненужные пустые строки внутри пустых объектов и массивов, но этого достаточно для моей цели.Например (используется сам по себе):
jq -Rr 'include "pretty"; pretty' test.json
, где функция сохраняется в pretty.jq
, а файл test.json
:
{"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]]]}}]}
дает:
{
"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
]
]
]
}
}
]
}