skip to Main Content

I am facing a problem, transforming a JSON .
Requirement is to concatenate only if my two columns are not Null, default value if any of one column is NULL

Input 1 :

[
  {
    "division_Code": "F",
    "department_Code": "L3D4",
    "department": "BABY ACCESSORIES"
  }
]

Output Expected :

{
  "PLMDepartment" : "FL3D4"
}

Input 2 :

[
  {
    "division_Code": "",
    "department_Code": "L3D4",
    "department": "BABY ACCESSORIES"
  }
]

Output Expected :

{
  "PLMDepartment" : "Invalid"
}

Input 3 :

[
  {
    "division_Code": "F",
    "department_Code": "",
    "department": "BABY ACCESSORIES"
  }
]

Output Expected :

{
  "PLMDepartment" : "Invalid"
}

Jolt spec I tried :

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "PLMDepartment": "=concat(@(1,division_Code),@(1,department_Code))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "PLMDepartment": "PLMDepartment"
      }
    }
  }
]

But its not coming as expected.

Pls help Can anyone who is a jolt expert, help me get the desired output. I think i m stuck in the last step

2

Answers


  1. You can use conditionally return the desired result after determining whether one of those attributes has zero size such as

    [
      {
        "operation": "modify-default-beta",
        "spec": {
          "*": {
            "szDvC": "=size(@(1,division_Code))",
            "szDpC": "=size(@(1,department_Code))",
            "Div1": "=divide(@(1,szDpC),@(1,szDvC))",
            "Div2": "=divide(@(1,szDvC),@(1,szDpC))",
            "PLMDepartment": "=concat(@(1,division_Code),@(1,department_Code))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "Div*": {
              "0.0": {
                "#Invalid": "PLMDepartment"
              },
              "*": {
                "@2,PLMDepartment": "PLMDepartment"
              }
            }
          }
        }
      },
      { // pick only one component among repeated ones if "PLMDepartment" is an array
        "operation": "cardinality",
        "spec": {
          "*": "ONE"
        }
      }
    ]
    
    Login or Signup to reply.
  2. The below spec can do the trick with less number of functions.

            [{
            "operation": "modify-default-beta",
            "spec": {
            "*": {
                "PLMDepartment": "=concat(@(1,division_Code),@(1,department_Code))"
            }
            }
        },
        {
            "operation": "shift",
            "spec": {
            "*": {
                "division_Code": {
                "": {
                    "#Invalid": "PLMDepartment"
                },
                "*": {
                    "@2,department_Code": {
                    "": {
                        "#Invalid": "PLMDepartment"
                    },
                    "*": {
                        "@4,PLMDepartment": "PLMDepartment"
                    }
                    }
                }
                }
            }
            }
        }
        ]
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search