3 ms·
If you ever need to fight against this annoying json trend (e.g., when your tool only emits certain information in stupid ungrepable json), consider filtering t
by enriquto 5y ago
If you ever need to fight against this annoying json trend (e.g., when your tool only emits certain information in stupid ungrepable json), consider filtering the output through a gron so that it becomes saner.
- dylan604 5y agowhy is JSON ungrepable? grep key file.json | awk -F: '{print $2}' if you're already searching for a key, seems like you're just wanting the value. granted, i hardly ever (have i actually ever??) interact with JSON this way, so i'm not exactly familiar with pitfalls.
- b3morales 5y agoWhen it's not formatted, just emitted as a single line. (Granted grep still works, but...not nicely.)
- dylan604 5y agogotcha. i'm imagining a pretty gnarly regex to return the data after the first colon after the match but only up until the next comma or square bracket or curly bracket. yeah, that's unpleasant. thankfully, there are tools like jq to help maintain one's sanity
- akyoan 5y agoTo be honest I find it funny that anyone would attempt to “parse” such a simple format with the wrong tool when there are plenty that can extract data in dot notation.
- fragmede 5y agoThe pitfall is that JSON has zero guarantees for how often line breaks do and don't occur, and is often used to represent hierarchical data. Grepping for 'key: foo', and some liberal use of -A and -B may find you what you're looking for, but grep is simply the wrong tool for that job. (And how do you handle a key with newlines in it?) jq [0] is the right tool, but jq's syntax is it's own, and is harder to use (unless you use it regularly). [0] https://stedolan.github.io/jq/ https://stedolan.github.io/jq/
- dylan604 5y agowhen all you have is a hammer, everything looks like a nail. i can see rudimentary attempts at trying to get some data on a system you might not have full control over and are just needing something. i'm sure we've all been logged into a remote system maintained by someone else. cavemen throwing rocks at the spaceship type scenarios.
- nrclark 5y agoJSON is greppable if all you need a simple key-value from a known format and indentation. It's much harder if you don't know the indentation/line breaks, or if it's whitespace-free, or if your key can ever appear in your data.
- throwawayboise 5y agoOr just: awk -F: '/key/ {print $2}' file.json
- redact207 5y agoJq makes life easier. I'd say for complex output, using filters is more readable than using awk to extract random positions of substrings
- trulyme 5y agoThank you, sounds very useful for quick queries: https://github.com/TomNomNom/gron https://github.com/TomNomNom/gron
- hencoappel 5y agoGrep is for simple text, jq allows much more powerful searching/selection. Don't get me wrong, I believe it should be a choice and not force JSON on users, but for some it's useful.