spiderman
spiderman

Reputation: 345

jq: error (at <stdin>:4): Cannot index string with string "ParameterKey"

test.sh is not replacing test.json parameter value and gives jq compilation error.

test.json

{
  "ParameterKey": "Project",
  "ParameterValue": "<check>"
 }

test.sh

cat test.json | jq \
'map(if .ParameterKey == "Project" 
    then . + {"ParameterValue" : "val" } 
else . end)' > result.json

Upvotes: 1

Views: 1147

Answers (1)

jq170727
jq170727

Reputation: 14695

The reason you're seeing that message is that the map() expression is enumerating the values of your object ("Project" and "<check>") invoking the if .ParameterKey ... expression on each of them. Unfortunately those are strings and the .ParameterKey part of your if condition won't work with string values so jq gives you the error Cannot index string with string “ParameterKey”.

You probably only want the map() if your test.json contains an array of objects. If your test.json contains an object at the top level such as {"ParameterKey": "Project", "ParameterValue": "<check>"} then you should remove the map(). e.g.

$ cat test.json | \
  jq 'if .ParameterKey == "Project" then . + {"ParameterValue" : "val" }  else . end'
{
  "ParameterKey": "Project",
  "ParameterValue": "val"
}

Upvotes: 2

Related Questions