bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

Condition Based adding a new Json element using JOLT #942

Open ASHISHTOM opened 4 years ago

ASHISHTOM commented 4 years ago

Suppose I have an input json with a few json elements like this.

{
  "billingsystem": {
    "profile": {
      "profile_type": "0",
      "risk_category ": "2",
      "credit_score": "200"
    }
  }
}

I want to add a new json element called "safe":"true" , when my json elements "risk_category " is equal to 2 and "credit_score" is equal to 200. Otherwise don't add any new element.

My new output json will be like:

{
  "billingsystem": {
    "profile": {
      "profile_type": "0",
      "risk_category ": "2",
      "credit_score": "200",
      "safe": "true"
    }
  }
}

Is it possible with jolt transform?? And what is the suitable spec for this requirement??

ASHISHTOM commented 4 years ago

I developed a spec myself to add a new json element called "safe":"true" if the credit score value matches 200. I am attaching the spec below: -

[
  {
    "operation": "shift",
    "spec": {
      "billingsystem": {
        "profile": {
          "@": "billingsystem.&",
          "credit_score": {
            "200": {
              "#true": "billingsystem.profile.safe"
            }
          }
        }
      }
    }
  }
]
zeshuai007 commented 4 years ago

Hi, @ASHISHTOM I use a function to splice two condition values into a key value, and then shift operation.

 [
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "billingsystem": {
        "profile": {
          "key":"=join('_', @(1,risk_category) ,  , @(1,credit_score))"
        }
      }
    }
  },{
    "operation": "shift",
    "spec": {
      "billingsystem": {
        "profile": {
          "profile_type": "billingsystem.profile.profile_type",
          "risk_category": "billingsystem.profile.risk_category",
          "credit_score": "billingsystem.profile.credit_score",
          "key": {
            "2_200": {
              "#true": "billingsystem.profile.safe"
            }
          }
        }
      }
    }
  }
  ]
zeshuai007 commented 4 years ago

I think Jolt is very good at structural transformation, but the comparison of correlation conditions does not perform very well. Maybe it can be enhanced on functions. What do you think about this?