jq:error(at <stdin>:4):无法使用字符串“ParameterKey”索引字符串

时间:2017-11-20 16:07:16

标签: jq

test.sh不替换test.json参数值并给出jq编译错误。

test.json

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

test.sh

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

1 个答案:

答案 0 :(得分:0)

您看到该消息的原因是map()表达式正在枚举您的对象(“Project”和“<check>”)的值,并调用{{1} }表达每个人。不幸的是,这些是字符串,if .ParameterKey ...条件的.ParameterKey部分不适用于字符串值,因此jq会为您提供错误if

如果您的Cannot index string with string “ParameterKey”包含一系列对象,您可能只需要map()。如果您的test.json包含顶级对象,例如test.json,那么您应该删除{"ParameterKey": "Project", "ParameterValue": "<check>"}。 e.g。

map()
相关问题