json 从数组中删除字段

kh212irz  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(168)

我必须将账户复制到输出中的两个级别。并且我需要从一个级别中删除fullAccountNumber。但是在我的jolt规范中,它是从两个级别中删除的。我在jolt中的删除规范中遗漏了什么吗?
我有以下输入

{
  "applicant": {
    "accounts": [
      {
        "fullAccountNumber": "00000000006276110961",
        "maskedAccountNumber": "XXXXXX0961",
        "accountType": "Checking"
      },
      {
        "fullAccountNumber": "00000000006276110961",
        "maskedAccountNumber": "XXXXXX0961",
        "accountType": "Saving"
      }
    ]
  }
}

字符串
期望输出

{
  "accounts": [
    {
      "fullAccountNumber": "00000000006276110961",
      "maskedAccountNumber": "XXXXXX0961",
      "accountType": "Checking"
    },
    {
      "fullAccountNumber": "00000000006276110961",
      "maskedAccountNumber": "XXXXXX0961",
      "accountType": "Saving"
    }
  ],
  "ma-loan": {
    "accounts": [
      {
        "maskedAccountNumber": "XXXXXX0961",
        "accountType": "Checking"
      },
      {
        "maskedAccountNumber": "XXXXXX0961",
        "accountType": "Saving"
      }
    ]
  }
}

震动:

[
  {
    "operation": "shift",
    "spec": {
       "applicant": {
          "accounts": ["accounts", "ma-loan.accounts"]
       }
    }
  },
  {
    "operation": "remove",
    "spec": {
       "ma-loan": {
          "accounts": {
             "*": {
                  "fullAccountNumber": ""
               }
           }
       }
    }
  }
]

电流输出:(从两个级别删除fullAccountNumber)

{
  "accounts": [
    {
      "maskedAccountNumber": "XXXXXX0961",
      "accountType": "Checking"
    },
    {
      "maskedAccountNumber": "XXXXXX0961",
      "accountType": "Saving"
    }
  ],
  "ma-loan": {
    "accounts": [
      {
        "maskedAccountNumber": "XXXXXX0961",
        "accountType": "Checking"
      },
      {
        "maskedAccountNumber": "XXXXXX0961",
        "accountType": "Saving"
      }
    ]
  }
}
cwdobuhd

cwdobuhd1#

我找到了我上面问题的解决方案。在我的例子中,remove 操作不起作用,我必须对第一个 shift 操作规范的结果使用 shift 操作。
解决方案1:

[
  {
    "operation": "shift",
    "spec": {
      "applicant": {
        "accounts": ["accounts", "ma-loan.accounts"]
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "ma-loan": {
        "*": "&1.&",
        "accounts": {
          "*": {
            "fullAccountNumber": {
              "*": {
                "@(2,maskedAccountNumber)": "&5.&4.[&3].maskedAccountNumber",
                "@(2,accountType)": "&5.&4.[&3].accountType"
              }
            }
          }
        }
      }
    }
  }
]

解决方案二:
解决方案改进不大,采用单 * 班 * 操作。

[
  {
    "operation": "shift",
    "spec": {
      "applicant": {
        "accounts": {
          "*": {
            "fullAccountNumber": {
              "@": "accounts[].&"
            },
            "*": ["accounts[&1].&", "ma-loan.accounts[&1].&"]
          }
        }
      }
    }
  }
]
zujrkrfu

zujrkrfu2#

我发现了另一种方法来得到这个输出JSON。我只是分享这个规范作为一个信息。我希望这个规范会帮助一些人。

质量标准

[
  {
    "operation": "shift",
    "spec": {
      "applicant": {
        "accounts": {
          "*": {
            "maskedAccountNumber": "Ma-loan.accounts[#2].maskedAccountNumber",
            "accountType": "Ma-loan.accounts[#2].accountType"
          },
          "@": "&"
        }
      }
    }
  }
]

所需输出

{
  "accounts" : [ {
    "fullAccountNumber" : "00000000006276110961",
    "maskedAccountNumber" : "XXXXXX0961",
    "accountType" : "Checking"
  }, {
    "fullAccountNumber" : "00000000006276110961",
    "maskedAccountNumber" : "XXXXXX0961",
    "accountType" : "Saving"
  } ],
  "Ma-loan" : {
    "accounts" : [ {
      "maskedAccountNumber" : "XXXXXX0961",
      "accountType" : "Checking"
    }, {
      "maskedAccountNumber" : "XXXXXX0961",
      "accountType" : "Saving"
    } ]
  }
}

相关问题