I'm trying to delete objects from a JSON payload if they have a key with a specific value
{
"data": [
{
"account": "12xUoMKwf12ABjNx4VCvYcNkX79gW1kzz2JnBLxkFbjswRczRvM",
"gateway": "113kQU96zqePySTahB7PEde9ZpoWK76DYK1f57wyhjhXCBoAu88",
"hash": "DTU1GGfR0eU15hv6KiV_bg6FOJXfUWz4TjIq1H7TGy4",
"timestamp": "2020-08-28T01:29:46.000000Z"
},
{
"account": "12xUoMKwf12ABjNx4VCvYcNkX79gW1kzz2JnBLxkFbjswRczRvM",
"gateway": "113kQU96zqePySTahB7PEde9ZpoWK76DYK1f57wyhjhXCBoAu88",
"hash": "l3EQR6AJ6R1qE1meHyafDnNF8vJ-X-rH1pujxQRTds4",
"timestamp": "2020-08-28T00:50:44.000000Z"
},
{
"account": "12xUoMKwf12ABjNx4VCvYcNkX79gW1kzz2JnBLxkFbjswRczRvM",
"gateway": "113kQU96zqePySTahB7PEde9ZpoWK76DYK1f57wyhjhXCBoAu88",
"hash": "5fQJY9MprH9b3IstVU1SdfBteUWoF_sdsVuiARPBtTY",
"timestamp": "2020-08-27T19:01:48.000000Z"
},
{
"account": "12xUoMKwf12ABjNx4VCvYcNkX79gW1kzz2JnBLxkFbjswRczRvM",
"gateway": "113kQU96zqePySTahB7PEde9ZpoWK76DYK1f57wyhjhXCBoAu88",
"hash": "0M0fudEmzW9dmAsO3dcWT286tTL6wTX9sllXtsyz-0Q",
"timestamp": "2020-08-27T18:15:17.000000Z"
}
]
}
I want to delete the entire object where .data[].hash
== 0M0fudEmzW9dmAsO3dcWT286tTL6wTX9sllXtsyz-0Q
to produce:
{
"data": [
{
"account": "12xUoMKwf12ABjNx4VCvYcNkX79gW1kzz2JnBLxkFbjswRczRvM",
"gateway": "113kQU96zqePySTahB7PEde9ZpoWK76DYK1f57wyhjhXCBoAu88",
"hash": "DTU1GGfR0eU15hv6KiV_bg6FOJXfUWz4TjIq1H7TGy4",
"timestamp": "2020-08-28T01:29:46.000000Z"
},
{
"account": "12xUoMKwf12ABjNx4VCvYcNkX79gW1kzz2JnBLxkFbjswRczRvM",
"gateway": "113kQU96zqePySTahB7PEde9ZpoWK76DYK1f57wyhjhXCBoAu88",
"hash": "l3EQR6AJ6R1qE1meHyafDnNF8vJ-X-rH1pujxQRTds4",
"timestamp": "2020-08-28T00:50:44.000000Z"
},
{
"account": "12xUoMKwf12ABjNx4VCvYcNkX79gW1kzz2JnBLxkFbjswRczRvM",
"gateway": "113kQU96zqePySTahB7PEde9ZpoWK76DYK1f57wyhjhXCBoAu88",
"hash": "5fQJY9MprH9b3IstVU1SdfBteUWoF_sdsVuiARPBtTY",
"timestamp": "2020-08-27T19:01:48.000000Z"
}
]
}
Is this possible to do? The size of the object can vary and have many different keys in the payload. The one thing that is constant and unique is the .data[].hash
key
Thanks
CodePudding user response:
You can use del along with select function such as
jq 'del(.data[] | select(.hash == "0M0fudEmzW9dmAsO3dcWT286tTL6wTX9sllXtsyz-0Q"))'
CodePudding user response:
If the goal really is:
to delete objects from a JSON payload if they have a key with a specific value
where the key is hash
,
then you could use walk
like so:
walk(if type=="object" and
.hash == "0M0fudEmzW9dmAsO3dcWT286tTL6wTX9sllXtsyz-0Q"
then empty else . end)
Notice that there is no reference to .data
in this program.