求助:使用JOLT将JSON嵌套对象转为数组适配NiFi处理
问题:将JSON中的嵌套对象转换为数组(适配NiFi SQL写入)
我有一个包含嵌套对象的JSON负载,需要将其中的嵌套对象修改为数组格式,以便NiFi处理数组元素并写入SQL。
输入JSON
[ { "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 } ]
期望输出JSON
[ { "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 } ]
尝试过的JOLT规格(未达预期)
[ { "operation": "shift", "spec": { "*": { "volume*": { "caption": "&.description", "freespace": "&.capacity_free", "name": "&.drive_name", "asset_id": "&.asset_id" } } } }, { "operation": "shift", "spec": { "*": "volumes[]" } } ]
正确的JOLT转换规格
[ { "operation": "shift", "spec": { "*": { // 保留顶层非嵌套字段 "architecture": "[#2].&", "hostname": "[#2].&", "memory": "[#2].&", "asset_id": "[#2].&", // 处理network前缀字段,收集到networks数组并添加name属性 "network*": { "@": "[#3].networks[]", "$": "[#3].networks[#2].name" }, // 处理volume前缀字段,收集到volumes数组并添加name属性 "volume*": { "@": "[#3].volumes[]", "$": "[#3].volumes[#2].name" } } } } ]
规格说明
- shift操作:通过路径匹配实现字段重组
- 顶层的
architecture、hostname等字段直接保留到原数组位置([#2]表示向上两层的数组索引) - 对
network*和volume*前缀的字段,先将整个对象放入对应数组(@表示当前节点值),再用$获取字段名作为name属性值 [#3]表示向上三层定位到顶层数组元素,[#2]表示当前嵌套对象在数组中的索引位置
- 顶层的
内容的提问来源于stack exchange,提问作者Marc
相关产品推荐
相关产品推荐

