当前位置: 代码迷 >> java >> 如何使用Jolt Specification将Json转换为Json?
  详细解决方案

如何使用Jolt Specification将Json转换为Json?

热度:106   发布时间:2023-07-25 19:07:59.0

我有这样的JSON负载;

   [ {
      "Samples" : {
        "Load" : [ {
          "dataItemId" : "a5",
          "timestamp" : "2019-02-17T04:58:44.097Z",
          "name" : "Aload",
          "sequence" : "19",
          "subType" : null,
          "content" : null
        }, {
          "dataItemId" : "a7",
          "timestamp" : "2019-02-17T04:58:44.097Z",
          "name" : "AAA",
          "sequence" : "19",
          "subType" : null,
          "content" : null
        } ],
        "Angle" : [ {
          "dataItemId" : "a6",
          "timestamp" : "2019-02-17T04:58:44.097Z",
          "name" : "Aact",
          "sequence" : "20",
          "subType" : "ACTUAL",
          "content" : null
        } ]
      }
    } ]

我想接收这样的JSON

{
      "Samples" : [
            {
              "tag_name": "Load",
              "dataItemId" : "a5",
              "timestamp" : "2019-02-17T04:58:44.097Z",
              "name" : "Aload",
              "sequence" : "19",
              "subType" : null,
              "content" : null
            }, {
              "tag_name": "Load",
              "dataItemId" : "a7",
              "timestamp" : "2019-02-17T04:58:44.097Z",
              "name" : "AAA",
              "sequence" : "19",
              "subType" : null,
              "content" : null
            }, {
              "tag_name": "Angle",
              "dataItemId" : "a6",
              "timestamp" : "2019-02-17T04:58:44.097Z",
              "name" : "Aact",
              "sequence" : "20",
              "subType" : "ACTUAL",
              "content" : null
            }
      ]
}

在我的场景中,我必须转换上面定义的每个json数据。 每秒接收500个JSON数据 如何使用Jolt Specification执行此操作? 震动规格很快吗? 适合流媒体吗? 还是我应该为此编写自己的脚本

如果您的有效载荷是具有单个对象的数组,则此规范适用:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Samples": {
          "*": {
            "*": {
              "$1": "Samples.&2[#2].tag_name",
              "*": "Samples.&2[#2].&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Samples": {
        "*": {
          "*": "Samples[]"
        }
      }
    }
  }
]

否则,您可能需要拆分数组,对每个元素进行转换,然后使用MergeContent或MergeRecord将它们重新组合在一起。 或者,您可以使用JoltTransformRecord将规范应用到数组中的每个元素/记录,只需更改规范即可在第一次shift操作中删除初始*部分。

  相关解决方案