这是Grouping Json with same value of a key with Jolt中问题的延续

我想用JOLT对输出SON进行以下更改.请帮助获取所需的SON输出.谢谢

  • 在输出json中 for each 对象添加"StartTime"
  • 将距离_3和距离_4转换为X_Z_距离和Y_Z_距离
  • Temp_1值四舍五入为2个小数点

输入SON:

[
  [
    {
      "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"
        }
      ]
    }
  ]
]

预期输出SON:


[
  {
    "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": ""
      }
    }
  }
]

推荐答案

您可以继续使用之前的分组技术,如下转换:

[
  { // you can pull all the expected attributes as some of them are arrays
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "@2,StartTime": "@1,LocationX.@1,LocationY.StartTime",
              "Location*": "@1,LocationX.@1,LocationY.&",
              "@DataValue": "@1,LocationX.@1,LocationY.@DataType", // match the values of the attributes as a whole
              "#D": "@1,LocationX.@1,LocationY.Distance_Type",
              "#M": "@1,LocationX.@1,LocationY.Distance_Length"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "distance_5?": null, // prepare this attribute to be removed
          "Temp_1": "=intSum(@(1,distance_3),-42)" // expected result attribute is an integer
        }
      }
    }
  },
  { // rename distance_3 and _4 
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&2_&1.&",
          "distance_3": "&2_&1.X_Z_distance",
          "distance_4": "&2_&1.Y_Z_distance",
          "distance_5": { "": "" } // reove this attribute
        }
      }
    }
  },
  { // pick only the first elements from the arrays
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  },
  { // derive an array of objects without a key
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
]

Json相关问答推荐

当有嵌套数组而没有嵌套数组时,展平JSON

将JSON输入子数组转换为字符串顺序列表输出

如何使用Aeson解码带有Unicode字符的JSON文件?

如何将响应数据保存到Flutter 中的共享首选项中

Spark-SQL中的from_unixtime函数未能给出正确的输出

jq - 仅在键值对存在的地方打印值

使用带有逗号的字段名称构建 struct

在 JSON 字符串中react i18n 换行符

使用 Spring 和 JsonTypeInfo 注释将 JSON 反序列化为多态对象模型

以 unicode 将 pandas DataFrame 写入 JSON

Rails 中奇怪的 JSON Javascript 问题

jQuery.getJSON 和 jQuery.parseJSON 返回 [object Object]?

如何将 Swift 对象转换为字典

如何使用 SwiftyJSON 将字符串转换为 JSON

使用 Node.js 对 JSON 中的字符串大小有限制吗?

如何将有向无环图 (DAG) 存储为 JSON?

jQuery JSON 响应总是触发 ParseError

Sequelize - 如何仅返回数据库结果的 JSON 对象?

使用 application/json 优于 text/plain 的优势?

JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题