5 ms·
What exactly was the benefit of removing quote characters? It does allow for easier grepping in some instances, but as you already covered, the brackets from a
by kbenson 9y ago
What exactly was the benefit of removing quote characters? It does allow for easier grepping in some instances, but as you already covered, the brackets from array notation generally need to be quoted as well. I think you're better off just assuming the need for single quotes when grepping this output, since the fact the output is valid javascript is very useful, IMO (especially with autovivification in JS), and losing that to make it slight easier to search is regression in my eyes.
But maybe I'm missing the benefit you're seeing, and it's not about searching?
- cryptonector 9y agoFirst off, you'd still have to escape newlines, and probably keep all other escapes required by JSON. But then the quotes would be unnecessary, thus wasteful. In particular, if I wanted to print a raw string (with escapes) at a particular path I could first use grep(1) to extract that path, then I'd have to write a fairly complex sed(1) command to first remove the path, then remove the quotes, whereas I could otherwise use grep(1) and cut(1) only. Mind you, I'm sticking to jq, as I know it really well. But I'm thinking of other users here. I think the value of a path-based transformation of JSON is ease of use, which motivates me to think about making it even easier to use, such as by removing those quotes.
- kbenson 9y ago> then I'd have to write a fairly complex sed(1) command to first remove the path No need for sed for the path, use cut for that as well. cut -d'=' -f2- will remove the path (but leave a space). In the end, you can accomplish it with the following, whichI think is fairly easy: echo 'json[0].foo.bar.baz = "some string";' | cut -d'=' -f2 | sed -e 's/^\s*"//' -e 's/";$//' For me, it's a toss up whether I would use that or Perl, since chances are I'm doing it as a first step in some other process, and I can just continue on in Perl for the rest of the process anyway. echo 'json[0].foo.bar.baz = "some string";' | perl -pE 's/^.*?"//; s/";$//;' I find keeping the output as valid JS extremely useful though, since I can just paste a grepped entry into a developer console to get a valid object to play with on a page. That's cutting out a pipe to a js prettifier, pipe to less, search for identifying text, and careful cut and paste to get the enclosing block of text for what ends up being a semi-common action for me. On the other hand, I can get raw strings, but barely ever have need of that, and could fairly easily make an alias for that if it became common.
- cryptonector 9y agoA single grep and cut would be simpler. You've proved my point :)