bazaarvoice / jolt

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

Element with comma separated values to Array #1191

Closed partner-Manoj-Muddu closed 1 year ago

partner-Manoj-Muddu commented 1 year ago

Hi,

I have below requirement.

siteID will have comma separated values. They should be split and created as different elements.

Input:

{ "Header": { "info1": "1", "info2": "2", "info3": "3" }, "Contract": { "contractNumber": "123", "contractLineItems": [ { "Role": [ { "businessPartnerID": "1", "businessPartnerRoleID": "11", "address": { "region": "CA", "country": "US" } }, { "businessPartnerID": "1", "businessPartnerRoleID": "22", "address": { "region": "CA", "country": "US" } } ], "siteID": "123,456,789", "paymentTerms": "PT" } ] } }

Expected output is

{ "Header": { "info1": "1", "info2": "2", "info3": "3" }, "Contract": { "contractNumber": "123", "contractLineItems": [ { "Role": [ { "businessPartnerID": "1", "businessPartnerRoleID": "11", "address": { "region": "CA", "country": "US" } }, { "businessPartnerID": "1", "businessPartnerRoleID": "22", "address": { "region": "CA", "country": "US" } } ], "siteID": "123", "paymentTerms": "PT" }, { "Role": [ { "businessPartnerID": "1", "businessPartnerRoleID": "11", "address": { "region": "CA", "country": "US" } }, { "businessPartnerID": "1", "businessPartnerRoleID": "22", "address": { "region": "CA", "country": "US" } } ], "siteID": "456", "paymentTerms": "PT" }, { "Role": [ { "businessPartnerID": "1", "businessPartnerRoleID": "11", "address": { "region": "CA", "country": "US" } }, { "businessPartnerID": "1", "businessPartnerRoleID": "22", "address": { "region": "CA", "country": "US" } } ], "siteID": "789", "paymentTerms": "PT" } ] } }

Could you please help me. Thank you

gawhadebhavna commented 1 year ago

Hi @partner-Manoj-Muddu

You can use the below spec

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "contractLineItems": {
          "*": {
            "siteIDSplitted": "=split(',',@(1,siteID))"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Header": "Header",
      "Contract": {
        "contractNumber": "Contract.contractNumber",
        "contractLineItems": {
          "*": {
            "siteIDSplitted": {
              "*": {
                "@(2,Role)": "Contract.contractLineItems.customMap&3&1.Role",
                "@": "Contract.contractLineItems.customMap&3&1.siteID",
                "@(2,paymentTerms)": "Contract.contractLineItems.customMap&3&1.paymentTerms"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "Contract": {
        "*": "Contract.&",
        "contractLineItems": {
          "customMap*": "Contract.contractLineItems.[]"
        }
      }
    }
  }
]
partner-Manoj-Muddu commented 1 year ago

Thank you. It is working