【问题标题】:Get outer json key value into very json in the nested json array using JOLT使用 JOLT 将外部 json 键值获取到嵌套 json 数组中的非常 json
【发布时间】:2017-04-19 05:49:26
【问题描述】:

我有一个这样的 JSON

{
  "id": "1234",
  "name": "something",
  "list": [
    {
      "A": "Something"
    },
    {
      "B": "Something1"
    }
  ]
}

我想要做的是将 idname 添加到 JSON 中的 list 我已经解决了几个问题,但我找不到有人这样做的地方。

【问题讨论】:

    标签: json apache-nifi jolt


    【解决方案1】:

    我相信以下 Shift 规范会起作用:

    {
        "id|name": "&",
        "list": {
          "*": {
            "@(2,id)": "&2.[&1].id",
            "@(2,name)": "&2.[&1].name",
            "*": "&2.[&1].&"
          }
        }
    }
    

    使用您的示例数据,产生的输出是:

    {
        "id": "1234",
        "name": "something",
        "list": [{
            "id": "1234",
            "name": "something",
            "A": "Something"
        }, {
            "id": "1234",
            "name": "something",
            "B": "Something1"
        }]
    }
    

    【讨论】:

    • 是的,我已经想通了,这就是我所做的。非常感谢!
    【解决方案2】:

    这个规范应该给你你想要的:

    [
      {
        "operation": "shift",
        "spec": {
          "list": {
            "*": {
              "@(2,id)": "&2.[&1].id",
              "@(2,name)": "&2.[&1].name",
              "*": "&2.[&1].&"
            }
          }
        }
      }
    ]
    

    根据您的输入,它会提供以下输出:

    {
      "list" : [ {
        "A" : "Something",
        "id" : "1234",
        "name" : "something"
      }, {
        "B" : "Something1",
        "id" : "1234",
        "name" : "something"
      } ]
    }
    

    【讨论】:

    • 哎呀没看到詹姆斯已经回答了,抱歉!看起来您可以在两者之间进行选择,具体取决于您是否想要传出 JSON 中的原始顶级字段,尽管您可以删除他的 id|name 条目并获得我所拥有的。支持他,我应该在提交之前刷新页面:)
    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多