- If the index would start from zero as default, and no need to
eliminate the case with Option = 'None of the above', then only
one step of spec would be sufficient such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&2.&&1"
}
}
}
}
]
Output :
{
"sales" : {
"Option0" : "Britannia",
"value0" : "0",
"Option1" : "Cadbury",
"value1" : "0",
"Option2" : "Parle",
"value2" : "0",
"Option3" : "None of the above",
"value3" : "0"
}
}
- if you want to get rid of the case with Option = 'None of the
above', then apply the following spec
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"Option": {
"None of the above": "",
"*": {
"@2": "&4.&3"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&1"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&2.&1.&"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&2.&&1"
}
}
}
}
]
Output :
{
"sales" : {
"Option0" : "Britannia",
"value0" : "0",
"Option1" : "Cadbury",
"value1" : "0",
"Option2" : "Parle",
"value2" : "0"
}
}
- While for your current case you can use the following multiple steps
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"Option": {
"None of the above": "",
"*": {
"@2": "&4.&3"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&1"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&2.&1.&",
"$": "&2.&1.idx"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"idx": "=intSum(1,@(1,idx))"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&2.@(1,idx).&"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "&2.&&1"
}
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"idx*": ""
}
}
}
]
Output :
{
"sales" : {
"Option1" : "Britannia",
"value1" : "0",
"Option2" : "Cadbury",
"value2" : "0",
"Option3" : "Parle",
"value3" : "0"
}
}
where the first two step is to be used to get rid of the case with Option = 'None of the above' . modify-overwrite-beta spec is to generate incremented indexes("idx"). Those indexes are sticked to the key names as suffixes, and then they're removed at the last spec.