jsonspecificationsjolt

Spec array transformation using jolt


i have two kinds of input the jsons

case 1:

{
  "MainObject": {
    "MainObjectDetails": {
      "SubDetails": {
        "ObjectProperty": "1"
      }
    }
  }
}

case 2:

{
  "MainObject": {
    "MainObjectDetails": {
      "SubDetails": {
        "ObjectProperty": [
          "1",
          "2"
        ]
      }
    }
  }
}

i want the resultant json to be an integer type array for both cases. How can it be done within one spec? expected result:

Case 1:

{
  "MainObject": {
    "MainObjectDetails": {
      "SubDetails": {
        "ObjectProperty": [
          1
        ]
      }
    }
  }
}

Case 2:

{
  "MainObject": {
    "MainObjectDetails": {
      "SubDetails": {
        "ObjectProperty": [
          1,
          2
        ]
      }
    }
  }
}

Solution

  • your problem can be solved by using two simple beta operations: =toList() and =toInteger().

    The input should look like this:

    {
      "MainObject": {
        "MainObjectDetails": {
          "SubDetails": {
            "ObjectProperty": [
              "1",
              "2"
            ]
          }
        }
      }
    }
    

    And the spec is this:

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "MainObject": {
            "MainObjectDetails": {
              "SubDetails": {
                "ObjectProperty": "=toList"
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "MainObject": {
            "MainObjectDetails": {
              "SubDetails": {
                "ObjectProperty": "=toInteger"
              }
            }
          }
        }
      }
    ]
    

    The first operation transforms the JSON in cases where the input looks like this: "ObjectProperty": "1"
    It converts it to: "ObjectProperty": [1]
    If the input doesn't match this case, it remains unchanged.

    Then the second spec casts every value of the array to an Integer.