354
votes

I'm using jq to parse a JSON file as shown here. However, the results for string values contain the "double-quotes" as expected, as shown below:

$ cat json.txt | jq '.name'
"Google"

How can I pipe this into another command to remove the ""? so I get

$ cat json.txt | jq '.name' | some_other_command
Google

What some_other_command can I use?

1
FYI, cat foo | bar is significantly less efficient than bar <foo or its equivalent <foo bar, especially if bar is a program like sort that can parallelize its operations when given a seekable file descriptor as opposed to a FIFO (which can only be read once front-to-back). It both means more startup overhead (invoking /bin/cat), and more context switches between userspace and kernel (each piece of content going through a read() within cat, then a write() to a FIFO in cat, and then a read() inside your destination program, instead of skipping to that last step directly).Charles Duffy
Another example of a case where the difference is a big one is cat foo | wc -c, vs wc -c <foo -- in the latter case it can just do two syscalls, seek() and tell(), to get the exact size of the file now matter how long it is; in the former, it needs to read to the end, even if that's gigabytes of content, because only cat has direct access to the original file, and wc has no way to request metadata on it.Charles Duffy

1 Answers

691
votes

Use the -r (or --raw-output) option to emit raw strings as output:

jq -r '.name' <json.txt