Binary addition using JOLT - json

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
}

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))"
}
}
]

Related

Nifi - Remove zeros(00) of my json in Nifi

Good Night.
I need to remove the zeros of "numero":
{
"cnpjemitente" : "48791685000168",
"numero" : "001262851",
"chavenfe" : "35221148791685000168550030012628511100259840",
"serie" : "3 "
}
The correct column:
{
"cnpjemitente" : "48791685000168",
"numero" : "1262851",
"chavenfe" : "35221148791685000168550030012628511100259840",
"serie" : "3 "
}
I need the leading zeros to be removed whenever possible.
Can anyone help me? Thanks
You can consecutively use the following within a JoltTransformJSON processor ;
toInteger and toString functions in a modify-overwrite-beta transformation spec
and then remove transformation in order to get rid of the auxiliary attribute "numero_"
such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"numero_": "=toInteger(#(1,numero))",// converts to 1262851(leading zeroes removed)
"numero": "=toString(#(1,numero_))"// converts to "1262851"(quoted)
}
},
{
"operation": "remove",
"spec": {
"numero_": ""
}
}
]
or use
toInteger and toString functions in each individual consecutive modify-overwrite-beta transformation spec
such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"numero": "=toInteger"// converts to 1262851
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"numero": "=toString"// converts to "1262851"
}
}
]

Nifi Jolt convert after subtract

[
{
"dst_cnt": "149125"
},
{
"src_cnt": "149136"
},
{
"TABLENAME": "NAME"
}
]
I want to subtract dst_cnt and src_cnt from this data with NIfi jolt. Is data operation possible after type conversion in NIfi?
Yes, it's possible. You can try the following transformatios spec
[
{
// combine individual attributes within the common object
"operation": "shift",
"spec": {
"*": {
"*": "&"
}
}
},
{
// sum up the the integer values after determining the negative form of "dst_cnt"
"operation": "modify-overwrite-beta",
"spec": {
"dst_cnt_": "=divide(#(1,dst_cnt),-1)",
"cnt_dif": "=intSum(#(1,dst_cnt_),#(1,src_cnt))"
}
},
{
// only pick the result derived from the subtraction
"operation": "shift",
"spec": {
"cnt_*": "&"
}
}
]

Converting List to Comma Separated String in JOLT

I have below scenario, where two operations need to be performed. One is parsing the list and create a comma separated string. Then, transform that into the output format json
Input -
{
"list": ["ABC","XYZ"]
}
Output -
{
"additionalAttributes" : {
"userContext" : [ {
"auths" : "ABC,XYZ"
} ]
}
}
Check this spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"list": "=join(',',#(1,list))"
}
}, {
"operation": "shift",
"spec": {
"list": "additionalAttributes.userContext[].auths"
}
}
]

How can I combine two arrays to create a key value pair with Jolt?

I've already created a spec to convert my JSON input
{
"rows": [
{
"row": [
"row1",
"row2",
"row3"
],
"header": [
"header1",
"header2",
"header3"
]
},
{
"row": [
"row4",
"row5",
"row6"
],
"header": [
"header4",
"header5",
"header6"
]
}
]
}
to convert to key-value pairs as following object result :
{
"header1" : "row1",
"header2" : "row2",
"header3" : "row3",
"header4" : "row4",
"header5" : "row5",
"header6" : "row6"
}
Is this possible to do using Jolt?
Is there a copy/paste error in your input? Judging by your desired output, the second object's header array should be ["header4", "header5", "header6"]. If that's the case, this spec should work:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"header": {
"*": {
"*": {
"#(3,row[#2])": "&"
}
}
}
}
}
}
}
]
One option is to use the following shift transformation spec :
[
{
"operation": "shift",
"spec": {
"*s": { // rows array
"*": {
"&(1,1)": { // row array
"*": {
"#": "#(3,header[&1])"
}
}
}
}
}
}
]
where
"*s": { stands for rows
"&(1,1)": { -> not immediate(zeroth) level but one more level up by using &(1, and grab the value there the first asterisk exists by &(..,1)
"#": "#(3,header[&1])" -> 3 levels needed as stated at the right hand side traverse the colon
as well in order to reach the level of &(1,1) which is used to
represent the "row" array along with &1 representation to go one level up the tree to reach the indexes of the array "row" while matching with the values of "row" through use of # on the left hand side
the demo on the site http://jolt-demo.appspot.com/ is :

Jolt Transformation

I am trying to write a jolt transformation for below input -
{
"restaurantId": "ZZ4ORJDY3E",
"chainId": "a-b"
}
expected output is -
{
"ZZ4ORJDY3E" : {
"key" : "ZZ4ORJDY3E",
"start" : "a",
"end" : "b"
}
}
My spec is -
[
{
"operation": "shift",
"spec": {
"#restaurantId": "#restaurantId.key",
"chainId": {
"*-*": {
"$(0,1)": "#restaurantId.start",
"$(0,2)": "#restaurantId.end"
}
}
}
}
]
The spec is not transforming as expected output. i want learn how to use attributes inside string parser.
Spec
[
{
"operation": "shift",
"spec": {
"restaurantId": {
// match any value of restaurantId
"*": {
// write the value to of the key $ to the output
// where the output is the "value of the key".key
// kinda hokey
"$": "&.key"
}
},
"chainId": {
"*-*": {
// write each part of the chainId to the output
// at the value of restaurantId from back up the tree
"$(0,1)": "#(3,restaurantId).start",
"$(0,2)": "#(3,restaurantId).end"
}
}
}
}
]