Home > Blockchain >  JOLT add a field in JSON in the middle of the structure
JOLT add a field in JSON in the middle of the structure

Time:10-30

I try append a new field in a JSON structure,i need it in a determined position, before the array but the SPEC locate the field in the end. This is my JSON original and the code of transformation written in JOLT TRANSFORMATION web:

INPUT:

{
  "Id": ">COS",
  "equipment": "ALA",
  "elementId": "M15463",
  "zone": "AMBA",
  "hub": "AVA",
  "terminalServer": "XS0156",
  "Area": "null",
  "timestamp": "1576155950000",
  "collectedData": [
    {
      "name": "llljljiouohh",
      "instance": "X1.M1.YE9.ON18",
      "value": "5",
      "unit": "db"
    }
  ]
}

JSON spec:

[
  {
    "operation": "default",
    "spec": {
      "timestamp_dt": "2022-10-14 15:00"
    }
  }
]

** result:**

{
  "Id": ">COS",
  "equipment": "ALA",
  "elementId": "M15463",
  "zone": "AMBA",
  "hub": "AVA",
  "terminalServer": "XS0156",
  "Area": "null",
  "timestamp": "1576155950000",
  "collectedData": [
    {
      "name": "llljljiouohh",
      "instance": "X1.M1.YE9.ON18",
      "value": "5",
      "unit": "db"
    }
  ],
  "timestamp_dt": "2022-10-14 15:00"
}

Expected:

{
  "Id": ">COS",
  "equipment": "ALA",
  "elementId": "M15463",
  "zone": "AMBA",
  "hub": "AVA",
  "terminalServer": "XS0156",
  "Area": "null",
  "timestamp": "1576155950000",
  "timestamp_dt": "2022-10-14 15:00",
  "collectedData": [
    {
      "name": "llljljiouohh",
      "instance": "X1.M1.YE9.ON18",
      "value": "5",
      "unit": "db"
    }
  ]
}

Any suggestion please? Thanks!

CodePudding user response:

You can individually write each key-value pairs in the desired order within a shift transformation such as

[
  {
    "operation": "default",
    "spec": {
      "timestamp_dt": "2022-10-14 15:00"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Id": "&",
      "equipment": "&",
      "elementId": "&",
      "zone": "&",
      "hub": "&",
      "terminalServer": "&",
      "Area": "&",
      "timestamp": "&",
      "timestamp_dt": "&",
      "collectedData": "&"
    }
  }
]
  • Related