I have a json file with set of data with repeating fields. I need to remove an entry with specific data of a field.
Json file:
JavaScript
x
21
21
1
[
2
{
3
"targets": [
4
"172.17.1.199"
5
],
6
"labels": {
7
"__meta_netbox_pop": "st-1742",
8
"__snmp_module__": "arista_sw"
9
}
10
},
11
{
12
"targets": [
13
"172.17.1.51"
14
],
15
"labels": {
16
"__meta_netbox_pop": "st-1754",
17
"__snmp_module__": "arista_sw"
18
}
19
}
20
]
21
The json file goes on and but this is an example of the whole json file.
I need to remove an entry of targets
with its labels
given a data of the target's
IP.
Input:
JavaScript
1
2
1
172.17.1.51
2
expected output:
JavaScript
1
12
12
1
[
2
{
3
"targets": [
4
"172.17.1.199"
5
],
6
"labels": {
7
"__meta_netbox_pop": "st-1742",
8
"__snmp_module__": "arista_sw"
9
}
10
}
11
]
12
Advertisement
Answer
Using jq
:
JavaScript
1
13
13
1
$ jq --arg ip 172.17.1.51 'map(select(.targets | contains([$ip]) | not ))' input.json
2
[
3
{
4
"targets": [
5
"172.17.1.199"
6
],
7
"labels": {
8
"__meta_netbox_pop": "st-1742",
9
"__snmp_module__": "arista_sw"
10
}
11
}
12
]
13