javajsonapache-nifijolt

Remove null from array using jolt


I have some issue removing the null values from an array using jolt as describe bellow:

Input

{
  "userId": "1",
  "age": "20",
  "firstName": "firstname1",
  "lastname": "lastname1",
  "zipCode": "zipcode1",
  "street": "street1",
  "city": "city1",
  "country": "country",
  "gender": "gender1",
  "grade": "grade1",
  "birthday": "birthday1"
}

Jolt spec

[
  {
    "operation": "shift",
    "spec": {
      "userId": "ID",
      "age": "age",
      "firstName": "firstName",
      "lastname": "lastname",
      "gender": "gender",
      "grade": "grade",
      "birthday": "birthday",
      "street|city|zipCode|country": {
        "$": "address[#2].code",
        "@": "address[#2].value"
      }
    }
  }
]

Output

{
  "ID": "1",
  "age": "20",
  "firstName": "firstname1",
  "lastname": "lastname1",
  "gender": "gender1",
  "grade": "grade1",
  "birthday": "birthday1",
  "address": [ null, null, null, null, null, null, null,
    {
      "code": "street",
      "value": "street1"
    },
    {
      "code": "city",
      "value": "city1"
    },
    {
      "code": "zipCode",
      "value": "zipcode1"
    },
    {
      "code": "country",
      "value": "country"
    }
  ]
}

I had some solutions suggested by @Barbaros thanks to him as describe in the link Remove null values from JSON output using Jolt

but still struggling with it, Any help would be appreciate.


Solution

  • You can prefix key-value pairs with a literal such as adr_ in the first step, and then use within the last step to distinguish the rest of the attributes those will be sybolized by "*":"&" pair such as

    [
      {
        "operation": "shift",
        "spec": {
          "userId": "ID",
          "*": "&",
          "street|city|zipCode|country": {
            "$": "adr_&.code",
            "@": "adr_&.value"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "adr_*": "address[]"
        }
      }
    ]
    

    where only "userId": "ID" written individually so as to rename it as desired.

    enter image description here