I have a problem writing objects to an array. Basically I want to merge the arrays and rename the fields, while keeping the objects seperately.
My input json looks like this:
{
"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"
}
]
}
This is my spec: (I am aware that the values in brackets are probably not right)
[
{
"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"
}
}
}
}
]
and this is my output:
{
"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"
}
]
}
}
while I expect output that looks like this:
{
"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"
}
]
}
}
I have major troubles understanding what to do and how the [#n]
work, I would really appreciate any help with fixing my spec and explaination why this does/does not work!
Need to distinguish the indexes of the arrays while combining them. To accomplish this, add a suffix letter or word(here I chose a
for a.[&1]
) for the first array while renaming all keys as desired within the first shift operation, then apply new extensions employees.bosses
in the consecutive shift operation such as
[
{
"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"
}
}
]