I am adding strings to a JSON array using jq and it works fine, but I would only like to add strings that don't exist yet. I experimented with unique, has, no, etc. I am missing a piece or two of puzzles.
Here is my initial json file, foo.json:
{ "widgets": [ { "name": "foo", "properties": [ "baz" ] }, { "name": "bar" } ] }
Here the jq command I built adds a line, even if it already exists:
$ cat foo.json | jq '.widgets[] | select(.name=="foo").properties |= .+ ["cat"]'
Here is the last iteration of my attempt.
$ cat foo.json | jq '.widgets[] | select(.name=="foo").properties | has("cat") | not | .properties += ["cat"]' jq: error: Cannot check whether array has a string key
brian source share