gayashanbc
gayashanbc

Reputation: 1024

jq - not working as expected in Jenkins shell script

I have the following JSON named as my.json.

[
  {
    "action": "copy",
    "artifact_location": "one foo one"
  },
  {
    "action": "copy",
    "artifact_location": "one bar one"
  },
  {
    "action": "remove",
    "artifact_location": "two foo two"
  }
]

My goal is to delete all the objects in the root JSON array if the artifact_location property of the a object contains the string value "foo".

I'm using jq command line utility to accomplish this task. Following is my jq command. It is working perfectly when I'm running it on my local machine (macOS and jq version is 1.6).

jq 'del(.[] | select(.artifact_location | test("foo")))' my.json

However, the above commands gives the following error when I try to run it as a shell script in a Jenkins job(Ubuntu and jq version is 1.3).

error: test is not defined
del(.[] | select(.artifact_location | test("foo")))
                                      ^^^^
1 compile error

What am I possibly doing wrong here?

Upvotes: 1

Views: 2042

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134521

test/0 is for testing if a string matches a particular regular expression, which is not available in jq 1.3 (as mentioned in the comments). contains/1 could be used in this case.

del(.[] | select(.artifact_location | contains("foo")))

I would rather approach this as filtering out the objects, rather than deleting them. Select objects that does not contain "foo".

map(select(.artifact_location | contains("foo") | not))

Upvotes: 4

Related Questions