How to transform data about PCs from TeemIP IPAM database, to feed it to Kea DHCP server as reservations with jq. This is the data I would like to transform
{
"objects": {
"PC::8": {
"code": 0,
"message": "",
"class": "PC",
"key": "8",
"fields": {
"name": "ntb",
"macaddress": "50:74:9d:b5:5f:5d",
"ipaddress_id_friendlyname": "10.1.1.6"
}
},
"PC::7": {
"code": 0,
"message": "",
"class": "PC",
"key": "7",
"fields": {
"name": "pc",
"macaddress": "00:11:c0:92:ab:0e",
"ipaddress_id_friendlyname": "10.1.70.70"
}
}
},
"code": 0,
"message": "Found: 2"
}
to this output
{
"hostname": "ntb",
"hw-address": "50:74:9d:b5:5f:5d",
"ip-address": "10.1.1.6"
},
{
"hostname": "pc",
"hw-address": "00:11:c0:92:ab:0e",
"ip-address": "10.1.70.70"
}
I've tried something like
cat pcs.json |jq '[.[]]|.[]|.[]|.[]|.[]|map(.)|{hostname: .name, hw-address: .macaddress, ip-address: .ipaddress_id_friendlyname}'
But I was not successful by any means. I'm total noob with json. Please help.
CodePudding user response:
Navigate to and iterate over the target items using .objects[].fields
, and construct your objects:
jq '
.objects[].fields | {
hostname: .name,
"hw-address": .macaddress,
"ip-address": .ipaddress_id_friendlyname
}
'
{
"hostname": "ntb",
"hw-address": "50:74:9d:b5:5f:5d",
"ip-address": "10.1.1.6"
}
{
"hostname": "pc",
"hw-address": "00:11:c0:92:ab:0e",
"ip-address": "10.1.70.70"
}
This produces a so-called stream of objects (no commas in between). If you rather wanted an array of objects (enclosed in square brackets, and its items delimited by commas), just surround the whole filter with a pair of brackets.