bazaarvoice / jolt

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

Value based changing the key in map - spec needed #855

Open yarabsyp opened 5 years ago

yarabsyp commented 5 years ago

Hello All,

Kindly help us to get a spec for this below requirement

Input :

{ "nameData": { "nameType": "I", "name1": "JACK", "name2": "Denial", "name3": "Jr", "freeFormat": "Y", "transactionCode": "I" } }

based on the freeFormat value i need to change key

If FreeFromat is "Y"

Desired output

{ "nameData": { "nameType": "E", -- need to change this value "ename1": "JACK", -- changing key "ename2": "Denial", -- changing key "ename3": "Jr", -- changing key "freeFormat": "Y", -- changing key "transactionCode": "I" -- no change } }

if FreeFromat is other then Y we should not change any values keep original map ..(freeFormat may not come sometime)

Thanks

wisthy commented 5 years ago

here you are. Just used a small trick to start by duplicating value of freeFormat to a custom field and putting a default value in it if not present. That way, I always have a value to do the test in the final shift and I know if it's coming from the input or from the default.

[
  {
    "operation": "shift",
    "spec": {
      "nameData": {
        "freeFormat": ["&1.&", "&1.tmp"],
        "*": "&1.&"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "nameData": {
        "tmp": "N"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "nameData": {
        "transactionCode|freeFormat": "&1.&",
        "tmp": {
          "Y": {
            "#E": "&3.nameType",
            "@(2,name1)": "&3.ename1",
            "@(2,name2)": "&3.ename2",
            "@(2,name3)": "&3.ename3"
          },
          "*": {
            "@(2,nameType)": "&3.nameType",
            "@(2,name1)": "&3.name1",
            "@(2,name2)": "&3.name2",
            "@(2,name3)": "&3.name3"
          }
        }
      }
    }
  }
  ]