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

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

Related

Binary addition using JOLT

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

Nifi - Replace values

Good Afternoon!
I have a JSON with:
{
"cnpjemitente" : "48791685000168",
"pedido" : "543306",
"pedidocliente" : { },
"emissao" : "20220912"
}
I need to replace the value "pedidocliente: {}" to:
{
"cnpjemitente" : "48791685000168",
"pedido" : "543306",
"pedidocliente" : null,
"emissao" : "20220912"
}
Sometimes the value will come in the field, I just want to send null when it is empty with '{}'.
How can I do it this way?
Thanks!
You can use a modify-overwrite-beta transformation spec within a JoltTransformJSON processor such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"pedidocliente": null
}
}
]
as you only need to change an individual attribute's value without affecting the others.
If it's the case that the value does not return always {}(an empty object), then rather use a shift transformation spec such as
[
{
"operation": "shift",
"spec": {
"pedidocliente": {
"*": "&1.&"
},
"*": "&"
}
}
]

How to delete the text befor the symbol ( _ ) , using jolt json?

i have an input JSON file , which have some attributes those contains some informations which i want to delete on the output
( example input : Hello_World => output = World )
For example this is the input :
{
"test": "hello_world"
}
and this is output needed :
{
"test" : "world"
}
the result derived from the jolt spec i tried is not even close, this is what i tried :
[
{
"operation": "modify-overwrite-beta",
"spec": {
"test": "=test.substring(test.indexOf(_) + 1)"
}
}
]
Sorry im i a newbie at jolt, just started it.
You can split by _ character within a modify transformation spec, and pick the one with the index 1 in order to display the last component of the arrah considering the current case of having a single underscore such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=split('_', #(1,&))"
}
},
{
"operation": "shift",
"spec": {
"*": {
"1": "&1" // &1 represents going one level up to reach to the level of the label "test" to replicate it
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :
Considering an input with attribute having value more than one underscore :
{
"test": "how_are_you"
}
[More generic] solution would be as follows :
[
{
"operation": "modify-overwrite-beta",
"spec": {
"test_": "=split('_', #(1,test))",
"test": "=lastElement(#(1,test_))"
}
},
{
// get rid of the extra generated attribute
"operation": "remove",
"spec": {
"test_": ""
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :

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