Home > Back-end >  Jolt transform add default value key value pairs if not present in json
Jolt transform add default value key value pairs if not present in json

Time:03-08

Ideally the input file should be of the follwing schema:

{
  "Name1": {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
  }
}

The ideal output :

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

key1 , key2, key3 ideally should be there, but there are cases where one of them or all might not be present. Example scenario:

{
  "Name1": {
    "key1": "value1"
  }
}

output :

{
  "key1": "value1",
  "key2": "",
  "key3": ""
}

Thus even if the field are not present , rather than ignoring these fields, just replace it empty string. Generally Jolt transform ignores the fields which are no present.

Kindly help me out with this.

CodePudding user response:

What you need is fulfilled by default transformation as the name implies such as the following one

[
  {
    "operation": "default",
    "spec": {
      "Name1": {
        "key1": "",
        "key2": "",
        "key3": ""
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Name1": {
        "*": "&"
      }
    }
  }
]

the demo is on the site enter image description here

  • Related