Nifi Jolt convert after subtract - json

[
{
"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_*": "&"
}
}
]

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

Can we perform multiplication using indexes of the list in jolt?

I'm trying to perform the multiplication of two different lists.
Is this possible to perform the multiplication corresponding indexes of the lists like list1[i] * list2[i].
Input JSON
{
"salesTaxAmount": [
5.2,
5.2,
5.2
],
"quantity": [
1,
2,
1
],
"unitAmount": [
20,
20,
20
]
}
Expected output
{
"totalAmount":[20,40,20]
}
Here I want the totalAmount like quantity * unitAmount.
Could someone help me, please.
Thanks!
There's no function such like multiply or product to be used within a modify spec but can be simulated by using divide function after reforming individual objects by a shift transformation such as
[
{
"operation": "shift",
"spec": {
"q*|u*": {
"*": "&.&1"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"prd_quantity": "=divide(1,#(1,quantity))",
"Amt_": "=divide(#(1,unitAmount),#(1,prd_quantity))",
"Amt": "=toInteger(#(1,Amt_))"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"Amt": "totalAmount[]"
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

transforming all string attributes which are boolean to true booleans

I thought this would be simple, perhaps it is but on my jolt learning journey i am once again struggling.
I have some json files (without a schema) which can be up to say 30Mb in size which have many thousands of string attributes at all levels of the document some of which (say 20%) which hold booleans as strings types.
I get that i can write a spec to pick out individual ones and convert them as per (post)[https://stackoverflow.com/questions/64972556/convert-boolean-to-string-for-map-values-in-nifi-jolt]
They technique wont work for me as nesting and levels are very arbitrary and there are simply way to many of them.
so how can i apply the data type transform to any attribute which has a boolean represented as a string ?
for example input
{
"name": "Fred",
"age": 45,
"opentowork" : "true",
"friends" : [
{
"name": "penny",
"closefriend": "false"
},
{
"name": "roger",
"farfriend": "true"
}
]
}
to desired
{
"name": "Fred",
"age": 45,
"opentowork" : true,
"friends" : [
{
"name": "penny",
"closefriend": false
},
{
"name": "roger",
"farfriend": true
}
]
}
I want to pick up attributes opentowork, closefriend and farfriend without explicity defining them int the spec, i also need to leave the values of the other attributes as they are (whatever level they are at).
You can use =toBoolean conversion just a bit separating case within the friends array by using "f*" representation from the else case "*" such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=toBoolean",
"f*": {
"*": {
"*": "=toBoolean"
}
}
}
}
]
or some multiple modify specs, without explicitly defining any attribute/array/object, might be added at the number of desired levels such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=toBoolean"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=toBoolean"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"*": "=toBoolean"
}
}
}
}
]

Jolt transform specification input

i have the following input json:
{
"tags": {
"event": "observation",
"source": "hunter"
}
}
The output JSON should look like below:
{
"tags" : [ "event:observation", "source:hunter" ]
}
can anyone provide any guidance on how to build a proper jolt specification for the above?
thank you very much for the help ^_^
You can use this specification
[
{ // combine each key-value pair under within common arrays
"operation": "shift",
"spec": {
"tags": {
"*": {
"$": "&2_&1",
"#": "&2_&1"
}
}
}
},
{ // concatenate key-value pairs by colon characters
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join(':',#(1,&))"
}
},
{
"operation": "shift",
"spec": { // make array key common("tags") for all arrays
// through use of _ seperator and * wildcard
"*_*": "&(0,1)"
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

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