JSON JOLT 转换

问题描述 投票:0回答:1

我需要使用 jolt 变换将给定的输入 JSON 转换为所需的输出 json

这是我输入的 JSON

{
  "fragmented": true,
  "nodeIp": "192.168.0.20",
  "requestId": "35daa014-d995-47ab-885b-0ce5ef24a214",
  "nodeId": "143898594",
  "timestamp": 1717413230106,
  "requestTimestamp": 1717413185926,
  "data": {
    "storage_controller": [
      {
        "ctlr_status": "up",
        "ctlr_name": "Top Controller",
        "ctlr_ip_address": "192.168.0.21",
        "ctlr_force_trap": "Empty",
        "ctlr_service_tag": "FSHVH82",
        "ctlr_asset_tag": "",
        "index": "1",
        "ctlr_leader": 1,
        "ctlr_number": 1,
        "ctlr_model": "CT_SC4020"
      },
      {
        "ctlr_leader": 2,
        "ctlr_ip_address": "192.168.0.22",
        "ctlr_model": "CT_SC4020",
        "ctlr_service_tag": "FSHVH82",
        "index": "2",
        "ctlr_number": 2,
        "ctlr_status": "up",
        "ctlr_name": "Bottom Controller",
        "ctlr_force_trap": "Empty",
        "ctlr_asset_tag": ""
      }
    ],
    "storage_controller_temp": [
      {
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 3,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 38,
        "ctlr_temp_name": "DIMM2",
        "index": "1.3"
      },
      {
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 4,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 45,
        "ctlr_temp_name": "PCIe Zone Inlet",
        "index": "2.4"
      },
      {
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 40,
        "ctlr_temp_name": "CPU Outlet",
        "index": "2.6",
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 6
      },
      {
        "ctlr_temp_current_c": 35,
        "ctlr_temp_name": "DIMM2",
        "index": "2.3",
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 3,
        "ctlr_temp_status": "up"
      },
      {
        "ctlr_temp_current_c": 35,
        "ctlr_temp_name": "DIMM1",
        "index": "2.2",
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 2,
        "ctlr_temp_status": "up"
      },
      {
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 2,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 38,
        "ctlr_temp_name": "DIMM1",
        "index": "1.2"
      },
      {
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 5,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 45,
        "ctlr_temp_name": "PCIe Zone Outlet",
        "index": "1.5"
      },
      {
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 1,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 62,
        "ctlr_temp_name": "CPU One",
        "index": "1.1"
      },
      {
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 6,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 43,
        "ctlr_temp_name": "CPU Outlet",
        "index": "1.6"
      },
      {
        "ctlr_temp_name": "BBU",
        "index": "1.7",
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 7,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 32
      },
      {
        "ctlr_temp_number": 4,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 48,
        "ctlr_temp_name": "PCIe Zone Inlet",
        "index": "1.4",
        "ctlr_temp_force_trap": "Empty"
      },
      {
        "index": "2.5",
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 5,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 41,
        "ctlr_temp_name": "PCIe Zone Outlet"
      },
      {
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 7,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 30,
        "ctlr_temp_name": "BBU",
        "index": "2.7"
      },
      {
        "ctlr_temp_name": "CPU One",
        "index": "2.1",
        "ctlr_temp_force_trap": "Empty",
        "ctlr_temp_number": 1,
        "ctlr_temp_status": "up",
        "ctlr_temp_current_c": 52
      }
    ]
  }
}

这是我想要得到的输出

{
  "fragmented": true,
  "nodeIp": "192.168.0.20",
  "requestId": "3b93f973-0b88-4464-8208-8ff6dc27189d",
  "nodeId": "143898594",
  "timestamp": 1717078433124,
  "requestTimestamp": 1717078414005,
  "data": {
    "storage_controller": [
      {
        "ctlr_asset_tag": "",
        "ctlr_leader": 2,
        "ctlr_status": "up",
        "ctlr_ip_address": "192.168.0.22",
        "ctlr_force_trap": "Empty",
        "ctlr_service_tag": "FSHVH82",
        "ctlr_number": 2,
        "ctlr_name": "Bottom Controller",
        "ctlr_model": "CT_SC4020",
        "index": "2",
        "temp_sensors": [
          {
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 48,
            "ctlr_temp_name": "CPU One",
            "index": "2.1",
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 1
          },
          {
            "index": "2.2",
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 2,
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 34,
            "ctlr_temp_name": "DIMM1"
          },
          {
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 34,
            "ctlr_temp_name": "DIMM2",
            "index": "2.3",
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 3
          },
          {
            "ctlr_temp_name": "PCIe Zone Inlet",
            "index": "2.4",
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 4,
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 44
          },
          {
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 5,
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 41,
            "ctlr_temp_name": "PCIe Zone Outlet",
            "index": "2.5"
          },
          {
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 6,
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 40,
            "ctlr_temp_name": "CPU Outlet",
            "index": "2.6"
          },
          {
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 29,
            "ctlr_temp_name": "BBU",
            "index": "2.7",
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 7
          },
          
        ]
      },
      {
        "ctlr_leader": 1,
        "ctlr_number": 1,
        "ctlr_status": "up",
        "ctlr_name": "Top Controller",
        "ctlr_ip_address": "192.168.0.21",
        "ctlr_force_trap": "Empty",
        "ctlr_model": "CT_SC4020",
        "ctlr_service_tag": "FSHVH82",
        "ctlr_asset_tag": "",
        "index": "1",
        "temp_sensors": [
          {
            "ctlr_temp_name": "CPU One",
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 1,
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 61,
            "index": "1.1",
            
          },
          {
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 2,
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 38,
            "ctlr_temp_name": "DIMM1",
            "index": "1.2",
            
          },
          {
            "ctlr_temp_current_c": 37,
            "ctlr_temp_name": "DIMM2",
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 3,
            "ctlr_temp_status": "up",
            "index": "1.3",
            
          },
          {
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 48,
            "ctlr_temp_name": "PCIe Zone Inlet",
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 4,
            "index": "1.4",
            
          },
          {
            "ctlr_temp_number": 5,
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 45,
            "ctlr_temp_name": "PCIe Zone Outlet",
            "ctlr_temp_force_trap": "Empty",
            "index": "1.5",
            
          },
          {
            "ctlr_temp_number": 6,
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 43,
            "ctlr_temp_name": "CPU Outlet",
            "ctlr_temp_force_trap": "Empty",
            "index": "1.7",
            
          },
          {
            "ctlr_temp_force_trap": "Empty",
            "ctlr_temp_number": 7,
            "ctlr_temp_status": "up",
            "ctlr_temp_current_c": 31,
            "ctlr_temp_name": "BBU",
            "index": "1.8",
            
          }
        ]
      }
    ]
  }
}

每个storage_controller都有storage_controller_temp,storage_controller_temp有索引键,其值为字符串,第一个数字表示它属于哪个storage_controller,第二个数字表示哪个storage_controller_temp,因此例如如果storage_controller_temp有索引“1.3”,则意味着ctlr_temp_number为3的storage_controller_temp属于ctlr_number为1的storage_controller

这是我尝试过的摇晃变换

[
  {
    "operation": "shift",
    "spec": {
      "fragmented": "fragmented",
      "nodeIp": "nodeIp",
      "requestId": "requestId",
      "nodeId": "nodeId",
      "timestamp": "timestamp",
      "requestTimestamp": "requestTimestamp",
      "data": {
        "storage_controller": {
          "*": {
            "*": "data.storage_controller[&1].&",
            "@(2,storage_controller_temp)": {
              "*": {
                "@": "data.storage_controller[&3].temp_sensors[]"
              }
            }
          }
        }
      }
    }
  }
]
json transform apache-nifi jolt
1个回答
0
投票

无需将最外层属性一一匹配,单个即可

"*":"&"

双人解决问题。

完整案例请参考以下转换规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",//the elements those are not beneath the "data" node
      "data": {
        "storage_controller": {
          "*": {
            "*": "&3.&2.@1,index.&"
          }
        },
        "storage_controller_temp": {
          "*": {
            "*": "&3.temp_sensors.@1,index.&1.&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "storage_controller": {
          "*": "&2.&1.&"
        },
        "temp_sensors": {
          "*.*": {
            "*": "&3.storage_controller.&(1,1).&2.&"//pick the 1st replacement of the dot separarted asterisks from 1 upper level by &(1,1)
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "storage_controller": {
          "*": {
            "*": "&2[#2].&",
            "temp_sensors": {
              "*": {
                "*": "&4[#4].&2[#2].&"
              }
            }
          }
        }
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.