I have a JSON formatted payload with nested objects that I want to modify into an array so Nifi can process the array elements pushing them into SQL. Input looks like this:
[
{
"architecture": "x64",
"hostname": "LAPTOP-B442R49A",
"memory": "32",
"network0": {
"asset_id": 1,
"defaultgateway": "192.168.1.99",
"dnsserver": "192.168.1.99",
"ipaddress": "192.168.1.239"
},
"network1": {
"asset_id": 1,
"defaultgateway": "",
"dnsserver": "",
"ipaddress": "192.168.56.1"
},
"volume0": {
"asset_id": 1,
"caption": "C:\",
"freespace": "89"
},
"volume1": {
"asset_id": 1,
"caption": "D:\",
"freespace": "0"
},
"asset_id": 1
}
]
Desired output is:
[
{
"architecture": "x64",
"hostname": "LAPTOP-B442R49A",
"memory": "32",
"networks": [
{
"name": "network0",
"asset_id": 1,
"defaultgateway": "192.168.1.99",
"dnsserver": "192.168.1.99",
"ipaddress": "192.168.1.239"
},
{
"name": "network1",
"asset_id": 1,
"defaultgateway": "",
"dnsserver": "",
"ipaddress": "192.168.56.1"
}
],
"volumes": [
{
"name": "volume0",
"asset_id": 1,
"caption": "C:\",
"freespace": "89"
},
{
"name": "volume1",
"asset_id": 1,
"caption": "D:\",
"freespace": "0"
}
],
"asset_id": 1
}
]
In the end we came up with the following spec (only for volumes):
[
{
"operation": "shift",
"spec": {
"*": {
"volume*": {
"caption": "&.description",
"freespace": "&.capacity_free",
"name": "&.drive_name",
"asset_id": "&.asset_id"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "volumes[]"
}
}
]
But that one does not get us what we want. Thanks for any help.
2
Answers
Hope the below Jolt spec will be helpful for you.
You can use separating the literals by asterikses and completing the missing spellings tecnique such as
the demo on the site https://jolt-demo.appspot.com/ is :