How do I transform below JSON using JOLT
- Input will be array of strings
- This string will be binary number , 0 or 1.
I want to perform binary addition and then split resultant array as split and every element should be a seperate property
{
"binaryarray": [
"0100",
"0010"
]
}
Output
{
"field1" : 0,
"field2" : 1,
"field3" : 1,
"field4" : 0
}
CodePudding user response:
I was able to partially achieve it
Convert array of string to array of int
perform Addition of array
Convert above to string
Split above string
[ { "operation": "modify-overwrite-beta", "spec": { "binaryarray": ["=toInteger", 0] } }, { "operation": "modify-overwrite-beta", "spec": { // // Sums "sumIntData": "=intSum(@(1,binaryarray))" } }, { "operation": "modify-overwrite-beta", "spec": { "sumIntData": ["=toString", 0] } }, { "operation": "modify-overwrite-beta", "spec": { "PID3": "=split('', @(1,sumIntData))" } } ]
CodePudding user response:
You can successively use split and max functions within modify transformation specs such as
[
{ //label the components of the arrays by their respective indexes(0,1) while converting them to independent attributes
"operation": "shift",
"spec": {
"*": {
"*": "&"
}
}
},
{ //convert those attributes again to arrays which has splitted elements one by one
"operation": "modify-overwrite-beta",
"spec": {
"*": "=split('',@(1,&))"
}
},
{ //transpose the arrays
"operation": "shift",
"spec": {
"*": {
"*": "&"
}
}
},
{ //perform binary sum through use of max function
"operation": "modify-overwrite-beta",
"spec": {
"*": "=max"
}
},
{ //rename the attributes
"operation": "shift",
"spec": {
"*": "field&"
}
}
]