0

I have to transform one JSON to another JSON, I am new to jolt.if you have any other methods in java please let me know. Input can have many other attributes in a nested manner. I have to make generic code which can consume all the fields in JSON and transform it into the desired output which I have mentioned.

Input

  {   
        "id": "123456789",
        "OrderType": "ABC",
        "Abc": [
           {
              "Name": "Pluto",
              "Value": "Charon"
           },
           {
              "Name": "Earth",
              "Value": "Moon"
           }
        ]
   }

Desired Output

  "MyFieldList": [
  {
    "Footer": "My Footer",
    "fieldList": [
      {
        "label": "id",
        "fieldName": "id",
        "fieldValue": "123456789",
        "editable": false, 
        "dataType": "STRING"
      },
      {
        "label": "OrderType",
        "fieldName": "OrderType",
        "fieldValue": "ABC",
        "editable": false, 
        "dataType": "STRING"
      },
      {
        "label": "Pluto",
        "fieldName": "Pluto",
        "fieldValue": "Charon",
        "editable": false, 
        "dataType": "STRING"
      },
      {
        "label": "Earth",
        "fieldName": "Earth",
        "fieldValue": "Moon",
        "editable": false, 
        "dataType": "STRING"
      }]
  }
  ]

I have tried using this jolt spec but , I cannot figure out the nested part how to flatten it .

 {
"operation": "shift",
"spec": {
    "*": {
      "$": "[#2].fieldName",
      "@": "[#2].fieldValue",
      "#false": "[#2].editable",
      "# ": "[#2].Size",
      "#STRING": "[#2].dataType"
    }
  }
 }
drtvhood
  • 17
  • 1
  • 7

1 Answers1

1

The important part is creating a array of arrays, before turning into the fieldList:

[
  {
    "operation": "shift",
    "spec": {
      "id": {
        "$": "[#1].[#1].fieldName",
        "@": "[#1].[#1].fieldValue",
        "#false": "[#1].[#1].editable",
        "#STRING": "[#1].[#1].dataType"
      },
      "OrderType": {
        "$": "[#2].[#1].fieldName",
        "@": "[#2].[#1].fieldValue",
        "#false": "[#2].[#1].editable",
        "#STRING": "[#2].[#1].dataType"
      },
      "Abc": {
        "*": {
          "Name": "[#3].[&1].fieldName",
          "Value": "[#3].[&1].fieldValue",
          "#false": "[#3].[&1].editable",
          "#STRING": "[#3].[&1].dataType"
        }
      }
    }
 },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "MyFieldList.fieldList.[]"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "MyFieldList": {
        "Footer": "My Footer"
      }
    }
  }
]

Matthew Warman
  • 3,234
  • 2
  • 23
  • 35