【问题标题】:How to filter keys by the value of another elements with JOLT?如何通过 JOLT 的另一个元素的值过滤键?
【发布时间】:2022-02-18 15:02:15
【问题描述】:

我有以下输入 JSON:

{
  "2021" : [ "a", "b", "c" ],
  "2022" : [ "d", "e", "f" ],
  "2023" : [ "g", "h", "i" ],
  "2024" : [ "j", "k", "l" ],
  "2025" : [ "m", "n", "o" ],
  "year" : "2022"
}

我需要过滤等于“年份”值的键。在这种情况下,我只需要密钥“2022”。类似的东西:

{
  "results_this_year" : [ "d", "e", "f" ],
  "year" : "2022"
}

【问题讨论】:

    标签: json jolt transformer


    【解决方案1】:

    这可以这样完成:

    [
      {
        "operation": "shift",
        "spec": {
          "year": {
            "*": {
              "@(2,&)": "&"
            },
            "@": "&"
          }
        }
      },
      // just to sort
      {
        "operation": "sort",
        "spec": {
          "*": ""
        }
      }
    ]
    

    "@(2,&)": "&" - 向后遍历源 JSON 并将顶层的值映射到 "year" 的值。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多