0

Im trying to insert a string ("/rest/config/hostgroup/${HGID}") into a json file using jq within a bash script.

initially it threw:

jq: error: syntax error, unexpected '/' (Unix shell quoting issues?) at , line 1:

So I set the string to be a jq variable like this:

echo $HOSTJSON | jq --arg HGP "$HGPATH" .hostgroup.ref=[$HGP] > host1.json

This didn't error but does not insert anything into the json other than [] as though the HGP variable isnt being populated. Omitting the square brackets around HGP throws the below indicating an empty variable:

jq: error: syntax error, unexpected $end (Unix shell quoting issues?) at , line 1: .hostgroup.ref=
jq: 1 compile error

I've tried this on the CLI and get the same results, can confirm HGPATH is populated:

echo $HGPATH
/rest/config/hostgroup/5
cat host.json | jq --arg HGP "$HGPATH" .hostgroup.ref=$HGP
jq: error: syntax error, unexpected $end (Unix shell quoting issues?) at <top-level>, line 1:
.hostgroup.ref=              
jq: 1 compile error

$HGID is being pulled from a query earllier in the script and is just a number and $HOSTJSON is a large JSON string

HGPATH="/rest/config/hostgroup/${HGID}"
echo $HOSTJSON | jq --arg HGP "$HGPATH" .hostgroup.ref=[$HGP] > host1.json
Sam
  • 161
  • 1
  • 14
  • 2
    You need to (single-)quote your jq command, otherwise `.hostgroup.ref=$HGP` is expanded to `.hostgroup.ref=` by bash – Aaron Nov 07 '19 at 16:50
  • @Aaron that worked thanks post it as the answer!? echo $HOSTJSON | jq --arg HGP "$HGPATH" '.hostgroup.ref=[$HGP]' > host1.json – Sam Nov 07 '19 at 16:57

1 Answers1

3

Before being executed, your jq --arg HGP "$HGPATH" .hostgroup.ref=$HGP command is first expanded by bash. It's useful for the "$HGPATH" part where you want jq to run with the value of the variable, but it's problematic for the .hostgroup.ref=$HGP, where bash doesn't have knowledge of any HGP variable and will replace $HGP by the empty string, resulting in the following command being executed :

jq --arg HGP /rest/config/hostgroup/5 .hostgroup.ref=

That explains the error message you're getting : jq complains it has unexpectedly reached the end of the command when parsing .hostgroup.ref=, which indeed isn't a correct jq command.

To avoid this, you will want to quote the jq command in single-quotes (which you should always do), telling bash to avoid expanding the command :

cat host.json | jq --arg HGP "$HGPATH" '.hostgroup.ref=$HGP'

As a side note, jq accepts an additional input file parameter, so you can write your pipeline as a single command :

jq --arg HGP "$HGPATH" '.hostgroup.ref=$HGP' host.json
Aaron
  • 24,009
  • 2
  • 33
  • 57