json 如果属性已经存在于主对象中,则不应从Jolt中的次对象引用

dddzy1tm  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(97)

在给定的JSON输入中,主要对象是“Account”。目标是创建一个Jolt规范,只有在“Account”对象中不存在属性时,才有选择地从“SystemResponse”对象中选择属性。例如,如果“serviceFlag”存在于“SystemResponse”中,但不存在于“Account”中,则应将其添加到“Account”。但是,如果“Account”对象中已存在“resellerFlag”(Map到“Account”中的“isReseller”)等属性,则应忽略这些属性。
请帮助我实现这个有条件的属性选择。

Input.json

{
  "Account": {
    "AccountInfo": {
      "isReseller": 10,
      "poRequired": 10
    }
  },
  "SystemResponse": {
    "resellerFlag": "false",
    "poRequiredFlag": "false",
    "serviceFlag": "true"
  }
}

输出.json

[
  {
    "operation": "shift",
    "spec": {
      "Account": {
        "AccountInfo": {
          "*": "Account.AccountInfo.&"
        }
      },
      "SystemResponse": {
        "@resellerFlag": {
          "false": {
            // Check if isReseller doesn't already exist in Account
            "Account.AccountInfo.isReseller": {
              "#0": "partyAccount.eqxAccountInfo.isReseller"
            }
          }
        },
        "poRequiredFlag": {
          "false": {
            // Check if poRequired doesn't already exist in Account
            "@Account.AccountInfo.poRequired": {
              "#0": "Account.AccountInfo.poRequired"
            }
          }
        },
        "serviceFlag": {
          "false": {
            // Check if poRequired doesn't already exist in Account
            "@Account.AccountInfo.isService": {
              "#0": "Account.AccountInfo.isService"
            }
          },
          "true": {
            // Check if poRequired doesn't already exist in Account
            "@Account.AccountInfo.isService": {
              "#1": "Account.AccountInfo.isService"
            }
          }
        }
      }
    }
  }
]

实际产量

{
  "Account" : {
    "AccountInfo" : {
      "isReseller" : 10,
      "poRequired" : 10
    }
  }
}

预期输出json

{
  "Account" : {
    "AccountInfo" : {
      "isReseller" : 10,
      "poRequired" : 10,
      "isService" : 1
    }
  }
}
lpwwtiir

lpwwtiir1#

您可以使用**~**运算符来处理此类问题。这意味着如果相应的属性不存在或为null,则与返回值匹配,例如在以下转换中应用

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Account": {
        "AccountInfo": {
          "~isReseller": "@(3,SystemResponse.resellerFlag)",
          "~poRequired": "@(3,SystemResponse.poRequiredFlag)", 
          "~serviceFlag": "@(3,SystemResponse.serviceFlag)"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Account": {
        "AccountInfo": {
          "*": {
            "true": {
              "#1": "&4.&3.&2"
            },
            "false": {
              "#0": "&4.&3.&2"
            },
            "*": { // leave the rest as they are
              "@1": "&4.&3.&2"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Account": {
        "AccountInfo": {
          "*": "=toInteger" // convert quoted integers unquoted
        }
      }
    }
  },
  { // rename the attributes as desired
    "operation": "shift",
    "spec": {
      "Account": {
        "AccountInfo": {
          "isReseller": "&2.&1.&",
          "poRequired": "&2.&1.isRequired",
          "serviceFlag": "&2.&1.isService"
        }
      }
    }
  }
]

网站http://jolt-demo.appspot.com/上的 demo 输入略有变化,如下所示:

相关问题