json - json 到 json 的 Jolt 转换删除键值并添加拆分字符串值
问题描述
我需要使用 jolt 转换来进行以下 JSON 转换:
需要删除密钥:
src-port
和tx-interval
要更改键的名称:
vdevice-host-name
至local-host-name
要在key:value处拆分key的字符串值vdevice-datakey
:
示例:
"vdevice-dataKey":"10.127.200.1-mpls-10.157.96.2-internet-ipsec"
至
"remote-system-ip : 10.157.96.2"
和"remote-color" : "internet"
输入 JSON 是:
{
"data": [{
"src-ip": "10.161.25.170",
"dst-ip": "10.161.25.182",
"vdevice-name": "10.127.200.1",
"color": "mpls",
"src-port": 12346,
"createTimeStamp": 1623334401569,
"system-ip": "10.157.96.2",
"dst-port": 12346,
"site-id": 141011085,
"transitions": 0,
"local-host-name": "AEXI630_XITO098_E_COUNTRY_2081",
"local-color": "mpls",
"detect-multiplier": "7",
"vdevice-dataKey": "10.127.200.1-mpls-10.157.96.2-internet-ipsec",
"@rid": 168682,
"vmanage-system-ip": "10.127.200.1",
"proto": "ipsec",
"lastupdated": 1623334401562,
"state": "up",
"tx-interval": 1000,
"uptime-date": 1623334260000
}, {
"src-ip": "10.161.25.170",
"dst-ip": "10.162.45.94",
"vdevice-name": "10.127.200.1",
"color": "mpls",
"src-port": 12346,
"createTimeStamp": 1623334402985,
"system-ip": "10.157.16.2",
"dst-port": 12346,
"site-id": 142011050,
"transitions": 0,
"vdevice-host-name": "AEXI630_XITO098_E_COUNTRY_2081",
"local-color": "mpls",
"detect-multiplier": "7",
"vdevice-dataKey": "10.127.200.1-mpls-10.157.16.2-internet-ipsec",
"@rid": 206290,
"vmanage-system-ip": "10.127.200.1",
"proto": "ipsec",
"lastupdated": 1623334401562,
"state": "up",
"tx-interval": 1000,
"uptime-date": 1623334260000
}]
}
输出 JSON 为:
{
"data": [{
"src-ip": "10.161.25.170",
"dst-ip": "10.161.25.182",
"vdevice-name": "10.127.200.1",
"color": "mpls",
"createTimeStamp": 1623334401569,
"system-ip": "10.157.96.2",
"dst-port": 12346,
"site-id": 141011085,
"transitions": 0,
"local-host-name": "AEXI630_XITO098_E_COUNTRY_2081",
"local-color": "mpls",
"detect-multiplier": "7",
"vdevice-dataKey": "10.127.200.1-mpls-10.157.96.2-internet-ipsec",
"@rid": 168682,
"vmanage-system-ip": "10.127.200.1",
"proto": "ipsec",
"lastupdated": 1623334401562,
"state": "up",
"uptime-date": 1623334260000,
"remote-system-ip":"10.157.96.2",
"remote-color":"internet"
}, {
"src-ip": "10.161.25.170",
"dst-ip": "10.162.45.94",
"vdevice-name": "10.127.200.1",
"color": "mpls",
"createTimeStamp": 1623334402985,
"system-ip": "10.157.16.2",
"dst-port": 12346,
"site-id": 142011050,
"transitions": 0,
"local-host-name": "AEXI630_XITO098_E_COUNTRY_2081",
"local-color": "mpls",
"detect-multiplier": "7",
"vdevice-dataKey": "10.127.200.1-mpls-10.157.16.2-internet-ipsec",
"@rid": 206290,
"vmanage-system-ip": "10.127.200.1",
"proto": "ipsec",
"lastupdated": 1623334401562,
"state": "up",
"uptime-date": 1623334260000,
"remote-system-ip":"10.157.16.2",
"remote-color":"internet"
}]
}
解决方案
您可以连续应用移位转换以生成要渲染的新元素,然后使用modify-overwrite-beta转换以将字符串拆分为多个片段以确定remote-system-ip和remote-color值,然后删除转换以删除所需的密钥以及最近生成的密钥,例如
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"vdevice-host-name": "&2.[&1].local-host-name",
"local-host-name": "&2.[&1].&",
"*": "&2.[&1].&",
"@(0,vdevice-dataKey)": "&2.[&1].remote"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"remote": "=split('-', @(1,&))",
"remote-system-ip": "@(1,remote[2])",
"remote-color": "@(1,remote[3])"
}
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"*": {
"src-port": "",
"tx-interval": "",
"remote": ""
}
}
}
}
]
推荐阅读
- wpf - 使用 ListCollectionView 时绑定 DataGrid Expander
- javascript - 不要在 Laravel 控制器(JS)中工作工具提示
- string - 以未知格式解码字符串
- html - Hosting Font Awesome Yourself:我们是否需要所有格式 eot、svg、ttf、woff、woff2?
- javascript - Javascript如何访问父模块中导入模块的数据(函数,变量)
- compiler-errors - 从源代码编译 gnumeric 时出现问题
- c# - 如何模拟工厂模式以支持单元测试?
- python - 如何使用隔离森林和结果分数正确识别异常?
- excel - 如何计算 COUNTIFS 中各个单元格的总和?
- url - 为什么我的内部测试轨道选择加入 URL 不起作用?