简体   繁体   中英

How to transform Json to Json using Jolt Specification?

I have JSON payload like this;

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

I want to receive JSON like this;

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

In my scenario, I must convert each json data defined above. I receive 500 JSON data per second . How can I do this using Jolt Specification ? Do Jolt Specification fast ? Do it suitable for streaming? Or Should I write my own script for this?

This spec works if your payload is an array with a single object:

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

Otherwise you might need to split the array, do the transform on each element, then use MergeContent or MergeRecord to get them back together. Alternatively you could use JoltTransformRecord which applies the spec to each element/record in the array, the spec would just need to changed to remove the initial * section in the first shift operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM