Search code examples
jsonapache-nifijolt

convert null values corresponding to an Array to empty array in nifi jolt


I want to achieve following JSON transformation using Jolt processor in NIFI By focusing on values field, in the first input in json (id 900551), values are populated as the following

input JSON

{
    "id": 900551,
    "internal_name": [],
    "values": [
        {
            "id": 1430156,
            "form_field_id": 900551,
            "pos": 0,
            "weight": null,
            "category": null,
            "created_at": "2020-10-15 12:55:02",
            "updated_at": "2020-11-27 10:45:09",
            "deleted_at": null,
            "settings": {
                "image": "myimage.png"
                "fix": false,
                "bold": false,
                "exclusive": false
            },
            "internal_value": "494699DV7271000,6343060SX0W1000,619740BWR0W1000",
            "css_class": null,
            "value": "DIFFERENCE",
            "settings_lang": {},
            "value_html": ""
        }
    ]
}

On the second input Json file to parse, values is null.

{
    "id": 900552,
    "internal_name": [],
    "values": []
}

I would like to convert null values to an empty array in my conversion

Is there a way to do this using existing Jolt operations ?

Thanks.


Solution

  • The default operation is what you are looking for:

    Defaultr walks the spec and asks "Does this exist in the data? If not, add it."

    In our case:

    if the value for "values" key is null, put the empty array instead

    Here is the spec:

    [
      {
        "operation": "default",
        "spec": {
          "values": []
        }
      }
    ]
    

    tested with https://jolt-demo.appspot.com/


    edit: answering the question from the comment:

    Maybe this workaround will work for you