首页 > 解决方案 > json 到 json 的 Jolt 转换删除键值并添加拆分字符串值

问题描述

我需要使用 jolt 转换来进行以下 JSON 转换:

需要删除密钥:

src-porttx-interval

要更改键的名称:

vdevice-host-namelocal-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"
    }]
}

标签: jsonjolt

解决方案


您可以连续应用移位转换以生成要渲染的新元素,然后使用modify-overwrite-beta转换以将字符串拆分为多个片段以确定remote-system-ipremote-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": ""
        }
      }
    }
  }
]

推荐阅读