How to remove double-quotes in jq output for parsing json files in bash?
148,032
Use the -r
(or --raw-output
) option to emit raw strings as output:
jq -r '.name' <json.txt

Author by
Chris F
I'm a professional software developer by day, dad/husband by night, and a performing musician on some weekends. I love ALL DevOps - it's not a job, it's my passion!
Updated on July 08, 2022Comments
-
Chris F 6 months
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? -
user541686 over 3 yearsWhat if I don't want to unescape anything and just want to strip the quotes?
-
user541686 over 3 yearsWhen I'm doing it right now, trying to output tab-separated fields as a nice table onto the terminal... I don't need or care to see the quotes, I know they're strings.
-
Charles Duffy over 3 years
[[ $string =~ ^["](.*)["]$ ]] && string="${BASH_REMATCH[1]}"
, say. -
user541686 over 3 yearsHuh? I need this for particular columns, I'm not trying to just strip 2 quotes from every single line. Are you saying there's no way to strip quotes from a field within jq?
-
Charles Duffy over 3 yearsIt sounds like you have detailed enough specifications that you should be asking a new question, so that specification can be given in full. This back-and-forth method of figuring out what you want (what your input is, what your expected output is, where the boundary between your jq and bash code lives, etc) is not helpful.
-
Charles Duffy over 3 yearsI answered the OP's answer to their satisfaction, as evidenced by the checkbox. You clearly, then, have a different one. I do not intend to continue this thread further; if you ask a well-specified question with explicit input and output and link to it, I may follow up there.
-
user541686 over 3 yearsYou don't care about anyone else landing on this page who had literally the question the OP asked as written? They can't second-guess what the OP meant when they're Googling.
-
Charles Duffy over 3 yearsIf you want to strip the quotes while remaining in jq, that distinguishes your question from the OP's, who wanted a string without quotes in bash. Once again, I'm done here until/unless you link to a new question.
-
hd1 over 1 yearIf you want to strip the quotes, just pipe the output from this command to
tr -d '"'
. -
Charles Duffy over 1 year@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 toMack "The Knife" Smith
, notMack The Knife Smith
. -
hd1 over 1 yearThe first comment says they " just want to strip the quotes", which I did. Corner cases like yours don't come into the discussion
-
Charles Duffy over 1 yearWhether 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) -
grim_i_am 10 monthsjq takes a filename so no need to pipe or redirect the file contents
jq -r '.name' json.txt
works just fine