by jolmg · 7y ago · 0 comments
jq -j '
[
[
paths(scalars)
| map(
if type == "number"
then "[" + tostring + "]"
else "." + .
end
) | join("")
],
[
.. | select(scalars) | @json
]
]
| transpose
| map(join(" = ") + "\n")
| join("")
'
EDIT: Got the string quoting and escaping.EDIT 2: For those who want to save this script, you can put just the jq code in an executable file with the shebang:
#!/usr/bin/jq -jf jq -r '
tostream
| select(length > 1)
| (
.[0] | map(
if type == "number"
then "[" + tostring + "]"
else "." + .
end
) | join("")
) + " = " + (.[1] | @json)
'
EDIT: For those who want to save this script, you can put just the jq code in an executable file with the shebang: #!/usr/bin/jq -rf jq -r '
( tostream
| select(length > 1)
| (
.[0] | map(
if type == "number"
then "[" + tostring + "]"
else "." + .
end
) | join("")
)
+ " = "
+ (.[1] | @json)
+ " |"
),
"."
' ( jq "$(sed 's/$/ |/;$a.')" <<< '{}' )
As in: catj example.json \
| ( jq "$(sed 's/$/ |/;$a.')" <<< '{}' ) \
> original.json jq -c --stream '
. as $in
| select(length == 2)
| (
$in[0] | map(
if type == "number"
then "[" + tostring + "]"
else "." + .
end
) | add
) + " = " + ($in[1] | tostring)'
Using `--stream` allows jq to start before parsing the entire json file. In my experience, a 700mb json file can take up 5gb of ram in either jq or python -m json. .movie.name = "Interstellar"
.movie.year = 2014
.movie.is_released = true
.movie.else = "Christopher Nolan"
.movie.cast[0] = "Matthew McConaughey"
.movie.cast[1] = "Anne Hathaway"
.movie.cast[2] = "Jessica Chastain"
.movie.cast[3] = "Bill Irwin"
.movie.cast[4] = "Ellen \\\\ Burstyn"
.movie.cast[5] = "Michael Caine"
You're outputting: ".movie.name = Interstellar"
".movie.year = 2014"
".movie.is_released = true"
".movie.else = Christopher Nolan"
".movie.cast[0] = Matthew McConaughey"
".movie.cast[1] = Anne Hathaway"
".movie.cast[2] = Jessica Chastain"
".movie.cast[3] = Bill Irwin"
".movie.cast[4] = Ellen \\\\ Burstyn"
".movie.cast[5] = Michael Caine"
Another point is how the strings at the right of the `=` are displayed. They should be quoted. The reason why they're not is because you piped the second element to `tostring` instead of `@json`.A better version of your suggestion would've been:
jq -r --stream '
select(length > 1)
| (
.[0] | map(
if type == "number"
then "[" + tostring + "]"
else "." + .
end
) | add
) + " = " + (.[1] | @json)
'
The use of `length > 1` instead of `length == 2` is a minor point, but if a future version jq decides to sometimes put 3 elements in these arrays, your filter would ignore those when we're likely to also want those. `length > 1` ensures what we need, that there are at least the elements that we're going to be using, while `length == 2` might filter some of those out, even if it's not right now.Your use of `add` is neat, though. I wouldn't have thought of that.
$ docker inspect 620f55df9177| structure.sh |grep -i addr
.[].NetworkSettings.GlobalIPv6Address
.[].NetworkSettings.IPAddress
.[].NetworkSettings.LinkLocalIPv6Address
.[].NetworkSettings.MacAddress
.[].NetworkSettings.Networks.bridge.GlobalIPv6Address
.[].NetworkSettings.Networks.bridge.IPAddress
.[].NetworkSettings.Networks.bridge.MacAddress
$ docker inspect 620f55df9177| jq .[].NetworkSettings.IPAddress
"192.168.0.2" jq -r '
tostream
| select(length > 1)
| (.[0] | map("[" + @json + "]") | join(""))
+ " = " + (.[1] | @json)
'
And this other option with the blacklist patterns: jq -r '
tostream
| select(length > 1)
| (
.[0] | map(
if type == "number" or (tostring | test("[@-]|^[0-9]|^else$"))
then "[" + @json + "]"
else "." + .
end
) | join("")
) + " = " + (.[1] | @json)
'
(The blacklist here is non-exhaustive, but an example.) jq -r '
tostream
| select(length > 1)
| (
.[0] | map(
if tostring | (
test("^[A-Za-z$_][0-9A-Za-z$_]*$")
and (
. as $property
| ["if", "else"] | all(. != $property)
)
)
then "." + .
else "[" + @json + "]"
end
) | join("")
) + " = " + (.[1] | @json)
'
You whitelist against what the syntax allows for identifiers and then you blacklist reserved keywords. Writing it this way makes it easier to verify for correctness when comparing with the ECMAScript Specs. This is still a non-exhaustive blacklist and the whitelist regex lacks allowed unicode characters. jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 3:
jq -j '
jq: 1 compile error
This, uh, doesn't work for me on jq-1.5.1.tostream | select(length > 1) | ( .[0] | map( if type == "number" then "[" + tostring + "]" else "." + . end ) | join("") ) + " = " + (.[1] | @json)
`#!/usr/bin/jq -rf ` with tostream wrapper in code works fine
jq "--stream -rf" path/to/script
and jq doesn't know of any one option called "--stream -rf".I haven't seen the discussions around these design decisions in the different OSes, but I imagine the crux of the matter is that you have to pick somewhere to stop, and where you chose to stop is largely arbitrary.
I mean, you can have the OS interpret shebangs with multiple arguments, but then you'll want to be able to put spaces in these arguments, so you'll want quoting, and then you'll want to put special characters like newlines inside, so you'll want escaping, etc.
The OS can implement all these things in execve()'s logic, but it might also be preferable to keep the logic simple in the interest of avoiding security-harming bugs. You know, less code, less bugs, less vulnerabilities.
If --stream had a single letter option equivalent, you could stick it together with the other ones. However, since it doesn't, your only option to make a portable script is to use a shell shebang like #!/bin/bash, and then do:
exec jq --stream -rf ...
You might feel that this single argument restriction sucks and is definitely inferior to any implementation of multiple argument shebangs. I don't know if macOS shebangs support quoting, but if they don't and simply split on spaces, then I can tell you they can't do hacky stuff like writing code in a shebang like this:> https://unix.stackexchange.com/questions/365436/choose-inter...
Granted, it's bad practice, but a little cool nevertheless.
From https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
> If the first line of a file of shell commands starts with the characters "#!", the results are unspecified.
There is no more specification for "#!/usr/bash" than "#!/usr/bin/jq -jf"
The exec page provides even fewer words about how to interpret shebangs if you thought perhaps I was linking to the wrong portion of the posix spec
Maybe not, but I'm pretty sure every system supports a single arg. And very few (none?) support more.