3

Given a JSON file, arguments.json:

{"dagger": true, "version": false, "nether_strike": true, 
 "greater_bash": "5", "FILE": "ancientscroll.txt", 
 "empower_haste": "1", "help": false}

When calling reading the variables as such:

#!/bin/bash
dagger=$(cat arguments.json | jq '.["dagger"]')
greater_bash =$(cat arguments.json | jq '.["greater_bash"]')    

echo $dagger
echo $greater_bash

[out]:

true
"5"

The variable contains the quotation marks "".

How to remove the quotes when reading a variable in jq?


I could use sed to strip the quotes as such:

greater_bash =$(cat arguments.json | jq '.["greater_bash"]' | sed -e 's/^"//' -e 's/"$//')
echo $greater_bash

[out]:

5

But is there a way to just strip the quotes when reading from jq?

4
  • 9
    jq -r for raw output
    – jeberle
    Sep 13, 2017 at 3:12
  • 1
    As jeberle indicated and as shown in the answer to your previous question, use jq -r for raw output i.e no extra quotes. Also for some reason jq occasionally decides it wants to give me colored output when I don't want it so I always use -M in my scripts to ensure jq doesn't add coloring codes. You may find it worthwhile to spend a moment reviewing the Invoking jq section of the online manual to see if any other options make sense for you.
    – jq170727
    Sep 13, 2017 at 4:10
  • Thanks @jq170727 !! The link to the manual is extremely helpful!
    – alvas
    Sep 13, 2017 at 9:03
  • 1
    Indeed. I also found the jq source very helpful as well, especially builtin.jq. There is also a lot of good information in the jq cookbook and on rosettacode
    – jq170727
    Sep 13, 2017 at 9:10

0

Browse other questions tagged or ask your own question.