我没遇到jq的新手,我已经学会了自己做大部分事情,但是我已经在键盘上敲了一下这个。看看下面的json
JSON:
{
"importType": "Upsert",
"immediateDeployment": false,
"isIgnoreNulls": false,
"isOverwriteNullsOnly": false,
"isPreferredandSync": false,
"outputLayout": {
"fields": [
{
"name": "TEMP_KEY",
"type": "String",
"length": "2000",
"displayName": "TEMP_KEY"
},
{
"name": "LoadSeqNum",
"type": "String",
"length": "2000",
"displayName": "LoadSeqNum"
}
]
}
}
我试图做的是.outputLayout.fields []我想创建一个名为" isIdentifier"的新对。如果.outputLayout.fields []。name是LoadSeqNum,则为true;如果不是,则为false,但我需要保持json的其余部分。因此,目标应如下所示:
目标:
{
"importType": "Upsert",
"immediateDeployment": false,
"isIgnoreNulls": false,
"isOverwriteNullsOnly": false,
"isPreferredandSync": false,
"outputLayout": {
"fields": [
{
"name": "TEMP_KEY",
"type": "String",
"length": "2000",
"displayName": "TEMP_KEY"
"isIdentifier": false
},
{
"name": "LoadSeqNum",
"type": "String",
"length": "2000",
"displayName": "LoadSeqNum"
"isIdentifier": true
}
]
}
}
我试过了:
jq '.outputLayout.fields[] | . + {"isIdentifier": (if (.name)=="LoadSeqNum" then true else false end)}'
但我当然错过了所有更高级别的东西。当我尝试做的时候:
.outputLayout.fields[].isIdentifier=(if (.outputLayout.fields[].name)=="LoadSeqNum" then true else false end)
我得到整件事两次,一次同时使用true,另一次同时使用false。我明白为什么要这样做,但我很难搞清楚什么会起作用。任何帮助或指向正确的方向?
答案 0 :(得分:1)
.outputLayout.fields[] |= (.isIdentifier = (.displayName == "LoadSeqNum") )
或等效但可能不那么隐蔽:
.outputLayout.fields |= map( .isIdentifier = (.displayName == "LoadSeqNum") )