如何通过在JSON数据中使用Jolt Spec文件将新项目添加到数组中



我在将新项目添加到数组时面临问题。我想在地址数组中添加新项目。任何帮助都会很感激。预先感谢:

这是我的代码:

input.json

{
  "lines": [
    {
      "movement": {
        "source": {
          "node": "Org_Node",
          "address": {
            "addressLine1": "abc",
            "addressLine2": "def",
            "addressLine3": "eg",
            "addressLine4": "abc123"
          },
          "recipient": {
            "firstName": "Ravi",
            "middleName": "",
            "lastName": "krishna"
          },
          "contactInformation": {
            "mobileNo": "9687568965"
          }
        }
      }
    }
  ]
}

在输出中,我想添加 city&状态:预期输出为:

{
  "address" : {
    "address1" : "abc",
    "address2" : "def",
    "address3" : "eg",
    "address4" : "abc123",
    "city":"ATP",
    "state":"AP"
  },
  "recipient" : {
    "firstName" : "Ravi",
    "middleName" : "",
    "lastName" : "krishna"
  },
  "contactInformation" : {
    "mobileNo" : "5036412733"
  }
}

我写下了以下规格文件以获取所需的输出,请让我知道我必须在哪里修改我是新手。spec.json文件:

    [{
  "operation": "shift",
  "spec": {
    "lines": {
      "*": {
        "movement": {
          "source": {
            "address": {
              "addressLine*": "[#5].&1.address&(0,1)",
              "*": "[#5].&1.&"
            },
            "recipient": {
              "*": "[#5].&1.&"
            },
            "contactInformation": {
              "*": "[#5].&1.&"
            }
          }
        }
      }
    }
  }
}]

spec

[
  {
    "operation": "shift",
    "spec": {
      "lines": {
        "*": {
          "movement": {
            "source": {
              // keeping the lines array
              // just keep the follow 3 fields
              "address|recipient|contactInformation": "lines[&3].&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      // default has a different syntax than shift.
      // You have to tell Default that lines is an array
      //  so that it can step into it correctly.
      "lines[]": {
        "*": {
          "address": {
            // apply cit and state defaults
            // not this is not in any way data driven
            // it will add these city and state defaults to everythhing
            // If you need it to be data driven, you have to deal with
            //  that before or after Jolt
            "city": "ATP",
            "state": "AP"
          }
        }
      }
    }
  }
]

最新更新