我在向数组中写入对象时遇到了一个问题。基本上我想合并数组并重命名字段,同时保持对象独立。
我的输入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]
的工作,我真的很感激任何帮助修复我的规范和解释为什么这不/不工作!
1条答案
按热度按时间6l7fqoea1#
在组合数组时需要区分数组的索引。为了实现这一点,为第一个数组添加后缀字母或单词(这里我选择
a.[&1]
作为a
),同时在第一个 shift 操作中根据需要重命名所有键,然后在连续的 shift 操作中应用新的扩展employees.bosses
,例如字符串