Check if null or empty in jolt and put another value which is present in input JSON

萝らか妹 提交于 2020-01-14 02:38:21

问题


This is my input JSON:

{
    "AddressBilling": {
        "FirstName": "Some Name",
        "LastName": "Some Name",
        "Address":"some address"
     }
    "AddressShipping": {
        "FirstName": "",
        "LastName": "",
        "Address":""
     }
}

I want to keep "AddressBilling" and "AddressShipping" but with different names i:e "payment_address" and "shipping_address", for which i have written a spec file for payment_address" part

{
    "operation": "shift",
    "spec": {
      "AddressBilling": {
        "FirstName": "payment_address.firstname",
        "LastName": "payment_address.lastname",
        "Address": "payment_address.address"
      },
       "AddressShipping": {
        "FirstName": "shipping_address.firstname",
        "LastName": "shipping_address.lastname"

      }
    }
}

Now what I want is to check if "Address" key in "AddressShipping" object is null then i want to copy "Address" of "AddressBilling" to "Address" of "shipping_address".


回答1:


Can do that with "modify-default". Modify-default will only fill in a value if a key does not exist or it's value is null.

Spec

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "shipping_address": {
        "address": "@(2,payment_address.address)"
      }
    }
  }
]

Input A, where there is not shipping address

{
  "payment_address": {
    "address": "some address"
  },
  "shipping_address": {}
}

Produces output A, where the billing address is copied over

{
  "payment_address" : {
    "address" : "some address"
  },
  "shipping_address" : {
    "address" : "some address"
  }
}

Input B, where there is a shipping_address

{
  "payment_address": {
    "address": "some address"
  },
  "shipping_address": {
    "address": 1234
  }
}

Produces output B, where the shipping address does not get overwritten.

{
  "payment_address" : {
    "address" : "some address"
  },
  "shipping_address" : {
    "address" : 1234
  }
}


来源:https://stackoverflow.com/questions/45411165/check-if-null-or-empty-in-jolt-and-put-another-value-which-is-present-in-input-j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!