Home > Software design >  jolt spec of the following input ? Thanks for the valueable time to answer
jolt spec of the following input ? Thanks for the valueable time to answer

Time:12-12

Here if Photos array in input JSON has multiple value then my current jolt spec is working fine but if Photos array in input JSON is empty like below and only Id1 and val field of input JSON IS MAPPED WITH Photos of output json then my second shift operation in jolt spec is failing. what will be the correct jolt spec that can handle both one value as well as multiple . input json

{
  "Entity": {
    "card": {
      "cardNo": "123456789",
      "status": "10",
      "cardAddress": "UK",
      "cardAddress1": "US",
      "cardCity": "mk",
      "name": "RAM",
      "lastName": "ABU",
      "Id1": 23452540,
      "val": "uu",
      "bd2": "78",
      "bal": "ii"
    },
    "Photos": []
  }
}

output json

{
  "tab" : {
    "text" : "123456789"
  },
  "address" : [ {
    "add" : "UK",
    "add2" : "US",
    "mk" : "mk"
  } ],
  "status" : "10",
  "Photos" : [ {
    "no" : "23452540",
    "caption2" : "uu"
  }, {
    "no" : "78",
    "caption2" : "ii"
  } ]
}


jolt spec used

[
  {
    "operation": "shift",
    "spec": {
      "Entity": {
        "card": {
          "cardNo": "tab.text",
          "status": "status",
          "cardAddress": "address[0].add",
          "cardAddress1": "address[0].add2",
          "cardC*": "address[0].mk",
          "Id1": "Photos.no",
          "val": "Photos.caption2",
          "bd2": "Photos.no",
          "bal": "Photos.caption2",
          "cf3": "Photos.no",
          "mal": "Photos.caption2",
          "lt4": "Photos.no",
          "tal": "Photos.caption2"
        },
        "Photos": {
          "*": {
            "Id": "Photos.no",
            "Caption": "Photos.caption2"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "tab": "&",
      "address": "&",
      "status": "&",
      "Photos": {
        "*": {
          "*": {
            "@": "&3[&1].&2"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Photos": {
        "*": {
          "no": "=toString"
        }
      }
    }
}
]

but if input is

{
  "Entity": {
    "card": {
      "cardNo": "123456789",
      "status": "10",
      "cardAddress": "UK",
      "cardAddress1": "US",
      "cardCity": "mk",
      "name": "RAM",
      "lastName": "ABU",
      "Id1": 23452540,
      "val": "uu"
    },
    "Photos": []
  }
}

then jolt spec is failing. for above input need ouput

{
  "tab" : {
    "text" : "123456789"
  },
  "address" : [ {
    "add" : "UK",
    "add2" : "US",
    "mk" : "mk"
  } ],
  "status" : "10",
  "Photos" : [ {
    "no" : "23452540",
    "caption2" : "uu"
  } ]
}

Thanks for answer. What changes are required in above jolt spec?

CodePudding user response:

Yes, it interestingly fails, might be a bug, I don't know. Rather you can try the following by adding [] to the end of the Photos.no and Photos.caption2 identifiers in order to handle both cases :

[
  {
    "operation": "shift",
    "spec": {
      "Entity": {
        "card": {
          "cardNo": "tab.text",
          "status": "&",
          "cardAddress": "address[0].add",
          "cardAddress1": "address[0].add2",
          "cardC*": "address[0].mk",
          "Id1": "Photos.no[]",
          "val": "Photos.caption2[]",
          "bd2": "Photos.no[]",
          "bal": "Photos.caption2[]",
          "cf3": "Photos.no[]",
          "mal": "Photos.caption2[]",
          "lt4": "Photos.no[]",
          "tal": "Photos.caption2[]"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "Photos": {
        "*": {
          "*": "&2[#1].&1"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Photos": {
        "*": {
          "no": "=toString"
        }
      }
    }
  }
]
  • Related