I want to know how to use shift and modify-overwrite-beta for the below input. Please explain how it works with your answer.
Input:
[
{
"data": {
"firstName": "Leanne",
"lastName": "Graham",
"id": 111
}
},
{
"data": {
"firstName": "Ervin",
"lastName": "Howell",
"id": 222
}
}
]
Desired output:
{
"111": {
"fullName": "Leanne Graham"
},
"222": {
"fullName": "Ervin Howell"
}
}
CodePudding user response:
You can solve that in different ways.
1. This spec is more understandable:
- Order your list with the
id
value as a key in theshift
operation:@(1,id)
- Concat
firstName
andlastName
in themodify-overwrite-beta
operation:=concat(@(1,firstName),' ',@(1,lastName))
- Remove unused keys in the
remove
operation:id, firstName, lastName
[
{
"operation": "shift",
"spec": {
"*": { // index of the array: 1, 2
"*": { // data object
"*": "@(1,id).&" // value of id in the current object: 111, 222
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": { // ids: 111, 222
"fullName": "=concat(@(1,firstName),' ',@(1,lastName))"
}
}
},
{
"operation": "remove",
"spec": {
"*": { // ids: 111, 222
"id": "",
"firstName": "",
"lastName": ""
}
}
}
]
2. This spec is shorter:
- Concat
firstName
andlastName
in themodify-overwrite-beta
operation:=concat(@(1,firstName),' ',@(1,lastName))
- Get the value of
id
as a key and putfullname
in it: key@(1,id).fullName
, value@(0,fullName)
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"fullName": "=concat(@(1,firstName),' ',@(1,lastName))"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"@(0,fullName)": "@(1,id).fullName"
}
}
}
}
]
The following image can help more:
CodePudding user response:
You can use those consecutive transformation specs
[
{
"operation": "shift",
"spec": {
"*": {// represents the level of the outermost object
"*": {// represents the level of the "data" object
"*": "@1,id.&"//make "id" values key of each object, "&" replicates each attribute's values
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"FullName": "=concat(@(1,firstName),' ',@(1,lastName))"// concatenate attributes through adding 1 level deep shift of the tree as staying on the right hand side, and name by the literal "FullName" on the left hand side
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"fi*Name|la*Name|id": "" //delete attributes "id" and the ones ending with "Names" except for the "fullName"
}
}
}
]
as the names suggest shift is used to move the attributes/arrays/objects to their new places, and modify updates on the existing structure