Home > OS >  JOLT spec to populate data based on tag presence
JOLT spec to populate data based on tag presence

Time:06-21

I have requirement where input will have one or two arrays based on scenarios:

Input 1:

{
  "name": "xyz",
  "age": "30",
  "addressA": [
    {
      "id": "111111",
      "details": ""
    },
    {
      "id": "111112",
      "details": ""
    }
  ],
  "addressB": [
    {
      "id": "222222",
      "details": ""
    },
    {
      "id": "222223",
      "details": ""
    }
  ]
}

Input 2:

{
  "name": "xyz",
  "age": "30",
  "addressB": [
    {
      "id": "222222",
      "details": ""
    },
    {
      "id": "222223",
      "details": ""
    }
  ]
}

So when 'addressA' is present in input json it should use that to give output if its no present then it should use 'addressB' to populate the same

Output expected:

For input 1:

[
  {
    "id": "111111",
    "details": ""
  },
  {
    "id": "111112",
    "details": ""
  }
]

For Input 2:

[
  {
    "id": "222222",
    "details": ""
  },
  {
    "id": "222223",
    "details": ""
  }
]

Any help would be appreciated.

I see option in JOLT to have if-else based on string value, but could not find anything to check just if exists

Thanks Mahendra

CodePudding user response:

You can determine the arrays A and B within the first shift transformation spec, and then use conditional within the second one such as

[
  {
    "operation": "shift",
    "spec": {
      "address*": {
        "*": "&(1,1)" // in order to determine the labels of the arrays as "A" and "B"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "A": {
        "@": ""
      },
      "*": "&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]
  • Related