I have a json that looks like:
{
"AlertDestinationAssociations": [
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": "SNMPv3Trap",
"SecurityName": "oneview_4861333356c624b597366648345"
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
}
],
"AlertDestinations": [
null,
"1.2.3.4",
null,
null,
null,
null,
null,
null
]
}
I want to edit the first null SNMPAlertProtocol and SecurityName. I then need to edit the corresponding array element in AlertDestinations. As a first step, I can change all of the null values with:
.AlertDestinationAssociations[] | select(.SNMPAlertProtocol == null).SNMPAlertProtocol |= "SNMPv3Trap" | .SecurityName |= "crap"
but I have not figured out how to limit that to just the first null element, nor how to edit the corresponding element in the AlertDestinations array. Does anyone have any suggestions?
CodePudding user response:
Using jq
jq --argjson pos 0 '
.AlertDestinationAssociations[$pos] |= {SNMPAlertProtocol: "SNMPv3Trap", SecurityName: "crap"} |
.AlertDestinations[$pos] |= "10.1.2.3"
' file.json
Output
{
"AlertDestinationAssociations": [
{
"SNMPAlertProtocol": "SNMPv3Trap",
"SecurityName": "crap"
},
{
"SNMPAlertProtocol": "SNMPv3Trap",
"SecurityName": "oneview_4861333356c624b597366648345"
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
},
{
"SNMPAlertProtocol": null,
"SecurityName": null
}
],
"AlertDestinations": [
"10.1.2.3",
"1.2.3.4",
null,
null,
null,
null,
null,
null
]
}