I was expecting some mention of jq for this post, and you did not disappoint. Thank you for the script -- it works great and I'm adding it to my collection.
This would be more suitable to large json files if used with the `--stream` flag. Here's my take on it:
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.
Thanks, but I haven't really done much in jq before. Doing the above script involved looking a lot through the manual and experimenting. This exercise was a learning experience for me. I was only able to do this because the manpage is well written.
macOS appears to support multiple args just fine. Which is why it annoys me that Shellcheck bitches about using more than one arg even though I'm writing a script for macOS specifically.
A hashbang is not defined by posix, so using it without args isn't "posix compatible".
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
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.
Hmm... I just downgraded to 1.5 and it seems to work. jq just has 2 numbers in its versioning[1]. The other number must be from your distribution's package building. Maybe the issue is with something your distribution did while building the package (like adding a patch)? It might also be that the syntax error is not with the script, but with the JSON you input. Sorry, without being able to reproduce the error, I can't help more.
Have you seen gron[0]? It's similar: flattens JSON documents to make them easily greppable. But it also can revert (ie, ungron) so you can pipe json to gron, grep -v to remove some data, then ungron to get it back to json.
Maybe GP meant that jq can do selection as well, i.e. that grepping is redundant after jq. But jq is much more complicated to learn and grep works on all inputs (not just json), so it makes a lot more sense to learn and use grep properly.
jq seems very powerful. I don't deal with json all that often and my most common use case (by far) is `jq '.' -C` and it took a few tries for me to remember that syntax.
The idea of flattening, grepping, then reverting sounds very appealing and sounds like a better fit for me.
I find it useful when I don't know what the json schema is.
Then you can just do a quick gron + grep and find where the interesting parts of a large json document are.
There are cases when you have some complicated json and just want to search for stuff. Then you use grep + gron. There are cases when you want a complete json processing tool. Then you use jq. You can probably simulate each approach with the other approach, but the code needed to this is just too tedious to write. So you use whatever tool fits your use case.
There is actually a standard around writing paths into JSON objects: JSON Pointer, https://tools.ietf.org/html/rfc6901. It’s straightforward, and avoids ambiguity between separator and key name by simple replacement, e.g. `/foo/bar~0baz~1quux` looks up a key named "foo", then a key named "bar~baz/quux" inside it. It’s not particularly widely used, but I’ve come across it in a few places over the years (it’s not a common thing to need to do), and probably most recently JMAP uses it for backreferences.
(I haven’t run it, but a skim of the code suggests that this tool will turn `{"foo.bar": "baz", "foo": {"bar": "baz"}}` into `["foo.bar"] = "baz"` and `.foo.bar = "baz"`, resolving the separator ambiguity in a pretty JavaScripty way.)
Comments (120) 40 threads
EDIT 2: For those who want to save this script, you can put just the jq code in an executable file with the shebang:
Continue this thread — 5 replies
Continue this thread — 2 replies
Maybe not, but I'm pretty sure every system supports a single arg. And very few (none?) support more.
Continue this thread — 2 replies
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
[1] https://github.com/stedolan/jq/releases
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
Continue this thread — 1 reply
[0] https://github.com/tomnomnom/gron
The project linked to is from 2014 with last update in 2015.and it is on NPM...
What is left to say? Thank you!
[1]:https://github.com/stedolan/jq
The idea of flattening, grepping, then reverting sounds very appealing and sounds like a better fit for me.
I don't think you really need neither `.` nor `-C`. Just `jq` seems to do the same colored output of the input by default.
Continue this thread — 3 replies
There are cases when you have some complicated json and just want to search for stuff. Then you use grep + gron.
There are cases when you want a complete json processing tool. Then you use jq.
You can probably simulate each approach with the other approach, but the code needed to this is just too tedious to write. So you use whatever tool fits your use case.
Continue this thread — 3 more replies
https://github.com/twpayne/flatjson
The flat format is great for diffs:
(I haven’t run it, but a skim of the code suggests that this tool will turn `{"foo.bar": "baz", "foo": {"bar": "baz"}}` into `["foo.bar"] = "baz"` and `.foo.bar = "baz"`, resolving the separator ambiguity in a pretty JavaScripty way.)
All comments shown.
Couldn't load more comments. Check your connection and try again.