Search code examples
javajsonxmlapache-nifijolt

JSON or XML with single element not coming as array problem solution through Jolt Transform?


Problem Statement :

While mapping XML or Json through Jackson or other Java libraries then arrays are incorrectly serialized in certain cases. i.e

When an array has more than 1 entry, it is serialized correctly

{
  "Car": {
    "data": [
      {
        "car": "Toyota"
      },
      {
        "car": "Maruti"
      }
    ]
  }
}

but when the array has only 1 entry, it is serialized not as an array, but as a single object.

{
  "Car": {
    "data": {
      "car": "Maruti"
    }
  }
}

Solution through Jolt Transform?


Solution

  • Figured out the solution for this problem through JOLT :

    INPUT 1 :

    {
      "Car": {
        "data": [
          {
            "car": "Toyota"
          },
          {
            "car": "Maruti"
          }
        ]
      }
    }
    

    INPUT 2:

    {
      "Car": {
        "data": [
          {
            "car": "Toyota"
          }
        ]
      }
    }
    

    JOLT SPEC :

    [
      {
        "operation": "cardinality",
        "spec": {
          "Car": {
            "data": "MANY"
          }
        }
      }
    ]