Home > Back-end >  add default keys in output for missing node object - jolt transform
add default keys in output for missing node object - jolt transform

Time:12-21

My Input request payload is as below:

{
  "attributes": {
    "training": {
      "attr1": "somevalue",
      "attr2": "somevalue"
    },
    "services": [
      {
        "recommendedService": "medical",
        "providedOrInitiated": 1
      },
      {
        "recommendedService": "engineering",
        "providedOrInitiated": 0
      }
    ]
  }
}

My desired output payload should be like:

{
  "attributes": {
    "training": { // Transform node if received in request
      "training1": "somevalue",
      "training2": "somevalue"
    },
    "services": { // Transform node if received in request
      "medicalProvided": 1,
      "engineeringProvided": 0,
      "caProvided": 0, // If not received in request, set to 0 for all missing keys
      "doctorProvided": 0,
      "teacherProvided": 0
    }
  }
}

Here is my json transformation spec:

[
  {
    "operation": "shift",
    "spec": {
      "attributes": {
        "ParentID": "customAttributesMap.ParentID",
        "training": {
          "attr1": "attributes.training.training1",
          "attr2": "attributes.training.training2"
        },
        "services": {
          "*": {
            "recommendedService": {
              "medical": {
                "@(2,providedOrInitiated)": "attributes.services.medicalProvided"
              },
              "engineering": {
                "@(2,providedOrInitiated)": "attributes.services.engineeringProvided"
              }
// How to set default for missing input fields.
            }
          }
        }
      }
    }
    }
]

I tried with default operation, but it adds keys even if services node is not received in request. That should not happen. Requirement is like transform node whichever is present in request. And only that should be sent in output. That's why default operation is not useful here.

Overall requirement is like Inside "service" node

{
   "recommendedService": "<SERVICE_NAME>",
   "providedOrInitiated": 1 // value 1 or 0
}

e.g. "medical" service is present then map medicalProvided to beneath providedOrInitiated value otherwise set it as 0.

All the nodes are dynamic. They might present or not.

CodePudding user response:

You can consecutively use shift and modify transformations such as

[
  {
    "operation": "shift",
    "spec": {
      "attributes": {
        "training": {
          "attr*": "&2.&1.&1&(0,1)"
        },
        "services": {
          "*": {
            "@providedOrInitiated": "&3.&2.@recommendedService",
            "$": "&3.&2.medical",
            "#0": "&3.&2.engineering",
            "#00": "&3.&2.ca",
            "#000": "&3.&2.doctor",
            "#0000": "&3.&2.teacher"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "services": {
          "*": "=max" // the function returns the integer result, and that won't be the default(`0`) if really returned a value which's `1` 
        }
      }
    }
  }
]

CodePudding user response:

You can use this spec:

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "servicesTemp": {
          "medical": 0,
          "engineering": 0,
          "caProvided": 0,
          "doctor": 0,
          "teacher": 0
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&",
        "services": {
          "*": {
            "providedOrInitiated": "&3.&2.@(1,recommendedService)"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&",
        "services": {
          "*": "&2.services.&",
          "@(1,servicesTemp)": "&2.services"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "services": {
          "*": "=lastElement"
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*Temp": ""
      }
    }
  }
]
  • Related