【问题标题】:Jolt to Map input fields with conditions用条件映射输入字段
【发布时间】:2021-02-11 12:34:08
【问题描述】:

我正在尝试只需要将一个输入映射到输出的地方。

任何帮助或建议表示赞赏。

  • 如果 topicA.ownertopicZ.owner 都存在,则输出 owner.name 应映射到 topicZ.owner

  • 如果只有 topicA.owner 则输出 owner.name 应映射到 topicA.owner

  • 如果 topicZ.owner 只有那么输出 owner.name 应该映射到 topicZ.owner

输入:

{
  "topicA": {
    "owner": "topic_a_owner"
  },
  "topicZ": {
    "owner": "topic_z_owner"
  }
}

摇晃:

[
  {
    "spec": {
      "*": {
        "ta": "@(2,topicA.owner)",
        "za": "@(2,topicZ.owner)"
      }
    },
    "operation": "modify-default-beta"
  },
  {
    "operation": "shift",
    "spec": {
      "topicA": {
        "ta": "owner.name"
      },
      "topicZ": {
        "za": "owner.name"
      }
    }
  }
]

预期输出:

{
  "owner" : {
    "name" : "topic_z_owner"
  }
}

【问题讨论】:

    标签: json jolt


    【解决方案1】:

    您提到的三个条件可以简化为以下两个条件。

    • 如果 topicZ.owner 存在,(无论 topicA.owner 是否存在)然后输出 owner.name 应该被映射到 topicZ.owner(这会合并您的第一个和第三个条件)
    • 如果 topicA.owner 只存在,则输出 owner.name 应映射到 topicA.owner

    所以在此基础上,可以进行如下操作。

    1. 使用modify-default-beta 操作将topicA.owner 的值复制到topicZ.owner 字段topicZ.owner 不存在时
    2. 使用shift 操作将topicZ.owner 的值映射到输出中的owner.name 字段。
    [
      {
        "spec": {
          "topicZ": {
            "owner": "@(2,topicA.owner)"
          }
        },
        "operation": "modify-default-beta"
      },
      {
        "operation": "shift",
        "spec": {
          "topicZ": {
            "owner": "owner.name"
          }
        }
      }
    ]
    
    

    【讨论】:

      猜你喜欢
      • 2019-02-05
      • 2023-04-06
      • 2021-10-10
      • 2018-03-24
      • 2020-04-24
      • 1970-01-01
      • 2015-03-18
      • 2017-07-22
      • 2020-12-10
      相关资源
      最近更新 更多