根据条件在 Jolt 中追加两个字符串

问题描述 投票:0回答:1

输入json:

{
  "accountType": "Admin",
  "id": "aaab-ccc-ddd-eeeAAA",
  "relationship": "DIRECT",
  "accountInfo": {
    "partyTypeCode": "PARTNER",
    "source": "Facebbok",
    "internalAccount": true,
    "category": "Father"
  }
}

输出:

{
  "key":"aaab-ccc-ddd-eeeAAA~Admin",
  "relationship":"aaab-ccc-ddd-eeeAAA~Admin~0001"
}

这里

0001
是Father的相应代码

第二个场景, 输入 JSON:

{
  "accountType": "User",
  "id": "aaab-ccc-ddd-eeeAAA",
  "relationship": "DIRECT",
  "accountInfo": {
    "partyTypeCode": "PARTNER",
    "source": "Facebbok",
    "internalAccount": true,
    "category": "Father"
  }
}

输出:

{
  "key":"aaab-ccc-ddd-eeeAAA",
  "relationship":"aaab-ccc-ddd-eeeAAA~0001"
}

仅当管理员应附加时,键才不应包含附加。但 0001 应该附加到关系

输出1:

{
  "key":"aaab-ccc-ddd-eeeAAA~Admin",
  "relationship":"aaab-ccc-ddd-eeeAAA~Admin~0001"
}

输出2:

{
  "key":"aaab-ccc-ddd-eeeAAA",
  "relationship":"aaab-ccc-ddd-eeeAAA~0001"
}
json transform jolt
1个回答
0
投票

您可以使用以下转换

[
  { // conditional for the cases of "accountType" values
    "operation": "shift",
    "spec": {
      "accountType": {
        "Admin": {
          "@2,id|$": "key"
        },
        "*": {
          "@2,id": "key[]"
        }
      },
      "@accountInfo.category": "rel" // assign a new attribute for this value
    }
  },
  { // conditional for the cases of "accountInfo.category" values
    "operation": "shift",
    "spec": {
      "*": "&",
      "rel": {
        "Father": {
          "#~0001": "&2"
        },
        "*": {
          "# ": "&2"
        }
      }
    }
  },
  { // generate "relationship" attribute
    "operation": "modify-overwrite-beta",
    "spec": {
      "key": "=join(~,@(1,&))",
      "rel": "=concat(@(1,key),@(1,rel))",
      "relationship": "=trim(@(1,rel))" // remove trailing whitespace if exists
    }
  },
  { // get rid of the auxiliary attribute "rel"
    "operation": "remove",
    "spec": {
      "rel": ""
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.