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
Share:
148,032
Chris F
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, 2022

Comments

  • Chris F
    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
    user541686 over 3 years
    What if I don't want to unescape anything and just want to strip the quotes?
  • user541686
    user541686 over 3 years
    When 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
    Charles Duffy over 3 years
    [[ $string =~ ^["](.*)["]$ ]] && string="${BASH_REMATCH[1]}", say.
  • user541686
    user541686 over 3 years
    Huh? 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
    Charles Duffy over 3 years
    It 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
    Charles Duffy over 3 years
    I 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
    user541686 over 3 years
    You 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
    Charles Duffy over 3 years
    If 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
    hd1 over 1 year
    If you want to strip the quotes, just pipe the output from this command to tr -d '"'.
  • Charles Duffy
    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 to Mack "The Knife" Smith, not Mack The Knife Smith.
  • hd1
    hd1 over 1 year
    The 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
    Charles Duffy over 1 year
    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)
  • grim_i_am
    grim_i_am 10 months
    jq takes a filename so no need to pipe or redirect the file contents jq -r '.name' json.txt works just fine