Nifi - Replace values - json

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.&"
},
"*": "&"
}
}
]

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 - ignore(or remove) the first digit of JSON

I have a JSON with a variable that I need to ignore the first digit.
{
"destinatarioDTO" : {
"cnpj" : "01377071000170"
}
}
I got the variable with the EvaluateJsonPath.
I need to transform the result 01377071000170 into 1377071000170 (remove the first digit).
You can add a JoltTransformJSON processor along with the following spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"destinatarioDTO": {
"len_cnpj": "=size(#(1,cnpj))",
"cnpj": "=substring(#(1,cnpj),1,#(1,len_cnpj))" // extract the value starting from the second character(one with the index 1) till the end of the string
}
}
},
{
"operation": "remove",
"spec": {
"destinatarioDTO": {
"len_cnpj": "" // get rid of newly generated, auxiliary attribute
}
}
}
]

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 :

how to match a word from string in jolt transformation

I have start writing a jolt transformation, and getting challenge to match the word from the object value.
input JSON :
{
"application" : "android",
"issue" : "FoundCriticalBug",
}
now if the value in key "issue" is critical then print bug is "critical" else the value may comes like "FoundNetworkBug" or "FoundNoBug"
expected output :
{
"application" : "android",
"issue" : "FoundCriticalBug",
"bug" : "critical"
}
There would be a if-else condition applied but i didn't went further. do suggest.
You can use a hash symbol(#) for the case only when the issue is equal to
FoundCriticalBug(considering the three presented fixed values) in order to generate a line
"bug" : "critical" representing a new attribute as follows
[
{
"operation": "shift",
"spec": {
"*": "&",
"issue": {
"FoundCriticalBug": {
"#1": "&2",
"#critical": "bug"
},
"*": {
"#1": "&2"
}
}
}
}
]

Jolt spec for conditional presence of field

I have a scenario where I have two very similar inputs formats, but I need one Jolt spec to process both formats consistently.
This is input style 1:
{
"creationTime": 1503999158000,
"device": {
"ip": "155.157.36.226",
"hostname": "server-123.example.int"
}
}
and this is input style 2:
{
"creationTime": 1503999158000,
"device": {
"ip6": "2001::face",
"hostname": "server-123.example.int"
}
}
The only difference is that style 1 uses device.ip, and style 2 uses device.ip6. There will always be one or neither of those fields, but never both.
I want to simply extract the following:
{
"created_ts": 1503999158000,
"src_ip_addr": "....."
}
I need src_ip_addr to be set to whichever field was present out of ip and ip6. If neither field was present in the source data, the value should default to null.
Is this possible with a single Jolt spec?
A single spec with two operations.
Spec
[
{
"operation": "shift",
"spec": {
"creationTime": "created_ts",
"device": {
// map ip or ip6 to src_ip_addr
"ip|ip6": "src_ip_addr"
}
}
},
{
"operation": "default",
"spec": {
// if src_ip_addr does not exist, then apply a default of null
"src_ip_addr": null
}
}
]
I tried out the following and it worked for my requirements:
[
{
"operation": "shift",
"spec": {
"creationTime": "created_ts",
"device": {
// map both to src_ip_addr, whichever one is present will be used
"ip": "src_ip_addr",
"ip6": "src_ip_addr"
}
}
},
{
"operation": "default",
"spec": {
"src_ip_addr": null
}
}
]