将具有相同键值的 Json 分组并使用 Jolt 重命名键

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

这是 使用 Jolt 将具有相同键值的 Json 进行分组

中提出的问题的延续

我想使用 JOLT 对输出 JSON 进行以下更改。您能帮助获得所需的 JSON 输出吗?谢谢你。

  • 为输出 json 中的每个对象添加“StartTime”
  • 将 istance_3 和 distance_4 重命名为 X_Z_distance 和 Y_Z_distance
  • 将 Temp_1 值四舍五入到小数点后 2 位

输入JSON:

[
  [
    {
      "Name": "03.04.2023",
      "StartTime": "2023-04-03 06:32",
      "SessionData": [
        {
          "LocationX": "36.282466",
          "LocationY": "-5.298164",
          "DataValue": "0.36",
          "DataType": "distance_1"
        },
        {
          "LocationX": "36.282466",
          "LocationY": "-5.298164",
          "DataValue": "11.0",
          "DataType": "distance_2"
        },
        {
          "LocationX": "36.282466",
          "LocationY": "-5.298164",
          "DataValue": "2231",
          "DataType": "distance_3"
        },
        {
          "LocationX": "36.282466",
          "LocationY": "-5.298164",
          "DataValue": "0.04",
          "DataType": "distance_4"
        },
        {
          "LocationX": "36.282466",
          "LocationY": "-5.298164",
          "DataValue": "58.82",
          "DataType": "distance_5"
        },
        {
          "LocationX": "36.278355",
          "LocationY": "-5.290660",
          "DataValue": "0.00",
          "DataType": "distance_1"
        },
        {
          "LocationX": "36.278355",
          "LocationY": "-5.290660",
          "DataValue": "9.8",
          "DataType": "distance_2"
        },
        {
          "LocationX": "36.278355",
          "LocationY": "-5.290660",
          "DataValue": "2206",
          "DataType": "distance_3"
        },
        {
          "LocationX": "36.278355",
          "LocationY": "-5.290660",
          "DataValue": "0.00",
          "DataType": "distance_4"
        },
        {
          "LocationX": "36.278355",
          "LocationY": "-5.290660",
          "DataValue": "58.28",
          "DataType": "distance_5"
        }
      ]
    }
  ]
]

预期输出 JSON:

[
  {
    "StartTime": "2023-04-03 06:32",
    "distance_1": "0.36",
    "LocationX": "36.282466",
    "LocationY": "-5.298164",
    "distance_2": "11.0",
    "X_Z_distance": "2231",
    "Y_Z_distance": "0.04",
    "Temp_1": 2189,
    "Distance_Type": "D",
    "Distance_Length": "M"
  },
  {
    "StartTime": "2023-04-03 06:32",
    "distance_1": "0.00",
    "LocationX": "36.278355",
    "LocationY": "-5.290660",
    "distance_2": "9.8",
    "X_Z_distance": "2206",
    "Y_Z_distance": "0.00",
    "Temp_1": 2164,
    "Distance_Type": "D",
    "Distance_Length": "M"
  }
]

震动:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "Location*": "@(1,LocationX).@(1,LocationY).&",
              "@DataValue": "@(1,LocationX).@(1,LocationY).@DataType"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "Temp_1": "=doubleSum(@(1,distance_3),-42)"
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "*": {
          "Distance_Length": "M",
          "Distance_Type": "D"
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": "ONE"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "distance_5": ""
      }
    }
  }
]
json apache-nifi jolt
1个回答
0
投票

您的初始 JOLT 规范未正确引用正确级别的 StartTime 字段。您试图在级别 * 访问它,但它应该在级别“0”。不包括将 distance_3 和 distance_4 分别重命名为 X_Z_distance 和 Y_Z_distance。

对 Temp_1 的修改不正确,并且您的 JOLT 规范缺少距离长度和距离类型的设置默认值。

我还删除了 distance_5,您的 JOLT 规范不包括删除所需输出中指定的 distance_5 键。

这是编辑后的 JOLT;

    [
  {
    "operation": "shift",
    "spec": {
      "*": {
        "0": {
          "StartTime": "[&1].StartTime",
          "SessionData": {
            "*": {
              "LocationX": "[&2].LocationX",
              "LocationY": "[&2].LocationY",
              "DataValue": {
                "distance_1": "[&3].distance_1",
                "distance_2": "[&3].distance_2",
                "distance_3": "[&3].X_Z_distance",
                "distance_4": "[&3].Y_Z_distance",
                "*": {
                  "@": "[&3].&"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "X_Z_distance": "=toInteger",
        "Y_Z_distance": "=toInteger",
        "distance_3": "=toDecimal",
        "Temp_1": "=toInteger(@(1,distance_3) * 100)"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "Distance_Length": "M",
        "Distance_Type": "D"
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.