Home > Blockchain >  How to move a key to up level from nested JSON object using JOLT Transformation
How to move a key to up level from nested JSON object using JOLT Transformation

Time:01-23

I want to do a JOLT Transformation and shift the key inside a nested JSON object to uplevel.

Here is my input JSON:

[
  {
    "deviceNumber": "1287391731_city",
    "country": {
      "State": [
        {
          "Priority": "MEDIUM",
          "City": {
            "Town": [
              {
                "unit": "",
                "type": "",
                "value": "Mumbai",
                "key": "District1"
              },
              {
                "unit": "",
                "type": "",
                "value": "Delhi",
                "key": "District2"
              },
              {
                "unit": "",
                "type": "",
                "value": "2",
                "key": "DistrictCount"
              }
            ]
          },
          "description": "describe the subcategory"
        }
      ]
    },
    "location": "8.21311,76.018231"
  },
  {
    "deviceNumber": "1287391731_city",
    "country": {
      "State": [
        {
          "Priority": "MEDIUM",
          "City": {
            "Town": [
              {
                "unit": "",
                "type": "",
                "value": "Bangalore",
                "key": "District1"
              },
              {
                "unit": "",
                "type": "",
                "value": "Chennai",
                "key": "District2"
              },
              {
                "unit": "",
                "type": "",
                "value": "2",
                "key": "DistrictCount"
              }
            ]
          },
          "description": "describe the subcategory"
        }
      ]
    },
    "location": "8.21311,76.018231"
  },
  {
    "deviceNumber": "1287391731_city",
    "country": {
      "State": [
        {
          "Priority": "MEDIUM",
          "City": {
            "Town": [
              {
                "unit": "",
                "type": "",
                "value": "Ahmedabad",
                "key": "District1"
              },
              {
                "unit": "",
                "type": "",
                "value": "Kolkata",
                "key": "District2"
              },
              {
                "unit": "",
                "type": "",
                "value": "2",
                "key": "DistrictCount"
              }
            ]
          },
          "description": "describe the subcategory"
        }
      ]
    },
    "location": "8.21311,76.018231"
  }
]

Here is the expected Output:

[
  {
    "@context": "https://my_context.in",
    "type": [
      "CITYVIEW"
    ],
    "deviceNumber": "1287391731_city",
    "DistrictCount": 2,
    "location": "8.21311,76.018231",
    "District1": {
      "NormalValue": "Delhi"
    },
    "District2": {
      "NormalValue": "Mumbai"
    }
  },
  {
    "@context": "https://my_context.in",
    "type": [
      "CITYVIEW"
    ],
    "deviceNumber": "1287391731_city",
    "DistrictCount": 2,
    "location": "8.21311,76.018231",
    "District1": {
      "NormalValue": "Bangalore"
    },
    "District2": {
      "NormalValue": "Chennai"
    }
  },
  {
    "@context": "https://my_context.in",
    "type": [
      "CITYVIEW"
    ],
    "deviceNumber": "1287391731_city",
    "DistrictCount": 2,
    "location": "8.21311,76.018231",
    "District1": {
      "NormalValue": "Ahmedabad"
    },
    "District2": {
      "NormalValue": "Kolkata"
    }
  }
]

Here districtCount is shifted uplevel and the value is shown as an integer. Type and context are default values and be created using default operation. District1 and District2 can be shifted using the following spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "country": {
          "*": {
            "*": {
              "*": {
                "*": ""
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "value": "@(1,key).NormalValue"
      }
    }
  }
]

This is the output I am getting:

{
  "District1": {
    "NormalValue": "Mumbai"
  },
  "District2": {
    "NormalValue": "Delhi"
  },
  "DistrictCount": {
    "NormalValue": "2"
  }
}

Could you please help me in writing the spec for JOLT transformation to get the expected output?

CodePudding user response:

You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "#https:\//my_context.in": "[&1].\\@context",
        "*": "[&1].&",
        "type": "[&1].&[]",
        "country": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "*": {
                    "*": {
                      "DistrictCount": {
                        "@(2,value)": "[&9].@2"
                      },
                      "District*": {
                        "@(2,value)": "[&9][email protected]"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "DistrictCount": "=toInteger"
      }
    }
  }
]
  • Related