712

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?

3
  • 11
    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). Commented Jun 20, 2017 at 14:58
  • 4
    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. Commented Jun 20, 2017 at 15:02
  • That is some really good info. Where can I read more about this?
    – ruevaughn
    Commented Dec 15, 2022 at 7:47

2 Answers 2

1289

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

jq -r '.name' <json.txt
9
  • 7
    If you want to strip the quotes, just pipe the output from this command to tr -d '"'.
    – hd1
    Commented May 12, 2021 at 1:00
  • 14
    @hd1, that's only true if there aren't literal quotes. If someone's name is "Mack \"The Knife\" Smith", you want it to change to Mack "The Knife" Smith, not Mack The Knife Smith. Commented May 12, 2021 at 1:51
  • 28
    Whether any given corner case is safe to ignore needs to be an explicit, case-by-case design consideration as a rule. Otherwise you end up in the world we live in, where software is riddled with bugs because the design didn't consider the full scope of possible inputs. Which is to say -- unless someone lays out as a requirement that " can never exist as literal data as opposed to syntax, it's irresponsible to assume such to be the case. (I work in security, and see sooo many issues misclassified as "input validation" failures when they're really failure to design for the full input domain) Commented May 12, 2021 at 20:32
  • 4
    jq takes a filename so no need to pipe or redirect the file contents jq -r '.name' json.txt works just fine
    – grim_i_am
    Commented Feb 17, 2022 at 8:21
  • 1
    @ucbpaladin, <json.txt reads from the file json.txt, whereas >json.txt empties the file json.txt and then writes to it. The OP here is using cat json.txt, so they want to read from the file, so < is correct. Commented Apr 21, 2023 at 18:12
67

So for a file containing just {"name": "Google"} then yes

sample='{"name":"Google"}'
echo $sample| jq '.name'

"Google"

using --raw-output helps

echo $sample| jq --raw-output '.name'

Google

But I stumbled upon this question because I was using --raw-output on a json array like this

sample='[{"name":"Yahoo"},{"name":"Google"}]'
echo $sample | jq --raw-output 'map(.name)'

[
  "Yahoo",
  "Google"
]

And I didn't understand why the quotes remained. I came across this post, and now I know adding | .[] does the trick!

echo $sample | jq --raw-output 'map(.name)| .[]'

Yahoo
Google
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.