benito_h
benito_h

Reputation: 560

jq fails to set value with colon in jenkins pipeline

My jenkins pipeline needs to manipulate .updateApp.json and set the name of an S3 bucket that contains colons as a value to the key BucketARNUpdate

What I tried up to now:

arn = "arn:aws:s3:::${PROJECT}-${STAGE}"
sh(script: 'jq  ".BucketARNUpdate=${arn}" .updateApp.json', returnStdout: true)

but this gives me

jq: error: syntax error, unexpected ':' (Unix shell quoting issues?) at <top-level>, line 1:

My second approach was using the --arg argument and I tried

sh(script: 'jq --arg var \"${arn}\" ".BucketARNUpdate=${var}" .updateApp.json', returnStdout: true)

but this gives

groovy.lang.MissingPropertyException: No such property: var

I tried it both with and without escaping the var value. How do I get this right?

Upvotes: 0

Views: 1115

Answers (1)

peak
peak

Reputation: 116880

One can only use the abbreviated form .foo if the key name is alphanumeric, it being understood that _ is counted as an alphabetic character here.

The basic form for referring to the value of a key named "KEY" is .["KEY"], but recent versions of jq also allow ."KEY".

Using your first approach and ignoring whatever escapes may be required by jenkins, you could write:

 ".[\"BucketARNUpdate=${arn}\"]"     

Your second approach, however, is perhaps safer. Again ignoring whatever escapes may be required by jenkins, the invocation would look like this:

jq --arg var "${arn}"  '.["BucketARNUpdate=" + $var]' .updateApp.json

Upvotes: 3

Related Questions