json Jolt -合并两个数组并重命名字段

oxalkeyp  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(302)

我在向数组中写入对象时遇到了一个问题。基本上我想合并数组并重命名字段,同时保持对象独立。
我的输入json看起来像这样:

{
   "board":[
      {
         "role":"Head of board", 
         "id":"111",
         "name":"John Snow"
      }
   ],
   "leaders":[
      {
         "role":"Accounting leader",
         "id":"222",
         "name":"Amanda Johns"
      },
      {
         "role":"HR leader",
         "id":"333",
         "name":"Frank Smith"
      }
   ]
}

字符串
这是我的规范:(我知道括号中的值可能不正确)

[
  {
    "operation": "shift",
    "spec": {
      "board": {
        "*": {
          "id": "employees.bosses[#2].emp_num",
          "role": "employees.bosses[#2].position",
          "name": "employees.bosses[#2].name"
        }
      },
      "leaders": {
        "*": {
          "id": "employees.bosses[#2].emp_num",
          "role": "employees.bosses[#2].position",
          "name": "employees.bosses[#2].name"
        }
      }
    }
  }
]


这是我的输出:

{
  "employees": {
    "bosses": [
      {
        "emp_num": ["111", "222"],
        "position": ["Head of board", "Accounting leader"],
        "name": ["John Snow", "Amanda Johns"]
      },
      {
        "emp_num": "333",
        "position": "HR leader",
        "name": "Frank Smith"
      }
    ]
  }
}


而我期望的输出看起来像这样:

{
  "employees": {
    "bosses": [
      {
        "emp_num": "111",
        "position": "Head of board",
        "name": "John Snow"
      },
      {
        "emp_num": "222",
        "position": "Accounting leader",
        "name": "Amanda Johns"
      },
      {
        "emp_num": "333",
        "position": "HR leader",
        "name": "Frank Smith"
      }
    ]
  }
}


我有很大的麻烦理解做什么和如何[#n]的工作,我真的很感激任何帮助修复我的规范和解释为什么这不/不工作!

6l7fqoea

6l7fqoea1#

在组合数组时需要区分数组的索引。为了实现这一点,为第一个数组添加后缀字母或单词(这里我选择a.[&1]作为a),同时在第一个 shift 操作中根据需要重命名所有键,然后在连续的 shift 操作中应用新的扩展employees.bosses,例如

[
  {
    "operation": "shift",
    "spec": {
      "board": {
        "*": {
          "id": "a.[&1].emp_num",
          "role": "a.[&1].position",
          "name": "a.[&1].name"
        }
      },
      "leaders": {
        "*": {
          "id": "&1.emp_num",
          "role": "&1.position",
          "name": "&1.name"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "employees.bosses"
    }
  }
]

字符串

相关问题