5 ms·
Appending | to each line and a last line . is a jq program that reproduces the original json. jq -r ' ( tostream | select(length > 1) | (
by hyperpallium 7y ago
Appending | to each line and a last line . is a jq program that reproduces the original json.
jq -r '
( tostream
| select(length > 1)
| (
.[0] | map(
if type == "number"
then "[" + tostring + "]"
else "." + .
end
) | join("")
)
+ " = "
+ (.[1] | @json)
+ " |"
),
"."
'
- jolmg 7y agoThat's insane. Here I was thinking about how much more challenging it would be to parse and reconstruct the object from jq, and you got the idea to take advantage of the syntax similarity to parse it as jq code itself. Nice. And so, that means the inverse of the jq code I posted would simply be: ( jq "$(sed 's/$/ |/;$a.')" <<< '{}' ) As in: catj example.json \ | ( jq "$(sed 's/$/ |/;$a.')" <<< '{}' ) \ > original.json
- hyperpallium 7y agoAnd a nice example of the path form being amenable to unix tools. BTW the input json can be null, so -n works (also using process substitution): jq -nf <(sed 's/$/ |/;$a.')
- sdegutis 7y agoEverything you guys have posted in this thread looks like pure ninja magic to me.
- cmonnow 7y agolooks like ancient Greek for us plebs
- hyperpallium 7y agoParsing is awkward in jq, but setpath(PATHS; VALUE) will create necessary structure. PATHS uses the array form, like ["movie", "cast", 5] not .movie.cast[5]. Since 1.5, jq has PCRE regex, so could remove ], and separate by . and [.
- deleted 7y ago[deleted]