2

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".

yousuf iqbal
  • 408
  • 2
  • 7
  • 16

1 Answers1

5

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
  }
}
Milo S
  • 4,466
  • 1
  • 19
  • 22