635

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?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Chris F
  • 14,337
  • 30
  • 94
  • 192

2 Answers2

1151

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

jq -r '.name' <json.txt
Dario Seidl
  • 4,140
  • 1
  • 39
  • 55
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 6
    If you want to strip the quotes, just pipe the output from this command to `tr -d '"'`. – hd1 May 12 '21 at 01:00
  • 10
    @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`. – Charles Duffy May 12 '21 at 01:51
  • The first comment says they " just want to strip the quotes", which I did. Corner cases like yours don't come into the discussion – hd1 May 12 '21 at 20:28
  • 19
    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) – Charles Duffy May 12 '21 at 20:32
  • 2
    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 Feb 17 '22 at 08:21
  • @grim_i_am: See [this](https://unix.stackexchange.com/a/368663/72707) to see where that idiom comes from. – Cbhihe Aug 20 '22 at 11:52
  • @grim_i_am that doesn't seem to work, jq returns: "jq: error: Could not open file json.txt: No such file or directory". The carrot works fine. – ucbpaladin Apr 21 '23 at 18:01
  • Isn't that arrow pointing the wrong way for this purpose? Works for me pointing to the filename. – ucbpaladin Apr 21 '23 at 18:04
  • @ucbpaladin, `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. – Charles Duffy Apr 21 '23 at 18:12
44

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
akauppi
  • 17,018
  • 15
  • 95
  • 120
HeyWatchThis
  • 21,241
  • 6
  • 33
  • 41
  • Note that `echo $sample` is itself buggy. See [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) -- always `echo "$sample"`, with the quotes; or, even better, `printf '%s\n' "$sample"` – Charles Duffy Apr 21 '23 at 18:14