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?

alvas
  • 115,346
  • 109
  • 446
  • 738
  • 9
    `jq -r` for raw output – jeberle Sep 13 '17 at 03:12
  • 1
    As [jeberle](https://stackoverflow.com/users/867757/jeberle) indicated and as shown in the [answer to your previous question](https://stackoverflow.com/a/46187922/8379597), 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](https://stedolan.github.io/jq/manual/#Invokingjq) section of the online manual to see if any other options make sense for you. – jq170727 Sep 13 '17 at 04:10
  • Thanks @jq170727 !! The link to the manual is extremely helpful! – alvas Sep 13 '17 at 09:03
  • 1
    Indeed. I also found the [jq source](https://github.com/stedolan/jq/tree/master/src) very helpful as well, especially [builtin.jq](https://github.com/stedolan/jq/blob/master/src/builtin.jq). There is also a lot of good information in the [jq cookbook](https://github.com/stedolan/jq/wiki/Cookbook) and on [rosettacode](https://rosettacode.org/wiki/Category:Jq) – jq170727 Sep 13 '17 at 09:10

0 Answers0