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

Related

Extract the value from an array in Apache NiFi

I have a JSON with this structure:
{
"documentos": [
{
"valorNota": 229.2,
"tipoDocumento": "PRE_NOTA_FISCAL",
"cnpjEmissorNota": "02130525000177",
"cnpjRemetente": "02130525000177",
"cnpjUnidade": "02130525000177",
"cnpjDestinatario": "14586674000124",
"valorFreteRealizadoNota": 0,
"serie": "1",
"valorFreteCalculadoNota": 1000,
"tipoOperacao": "SAIDA",
"dataEmissao": "2022-12-05 11:03:00",
"numeroNota": 744659
}
],
"dataEmissaoConhecimento": "2022-12-05 11:03:00.0",
"localizacaoDestinoConhecimento": 3002171,
"localizacaoOrigemConhecimento": 3001956,
"cepOrigemConhecimento": 44079006,
"cepDestinoConhecimento": 48880000,
"valorFreteRealizadoConhecimento": 0,
"oidConhecimento": 10802880097,
"valorFreteCalculadoConhecimento": 1000
}
I need to extract the value of "numeroNota" (can be with EvaluateJsonPath or other processor).
How can I do this?
Thanks
One option is to use a shift transformation spec within a JoltTransformJSON processor such as
[
{
"operation": "shift",
"spec": {
"docu*": { //the level of "documentos" array
"*": { //the indexes of the array
"numeroN*": "" //yields the value of the attribute without any wrapper
}
}
}
}
]
which returns 744659 as result for the current case

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 :

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

Is it possible to match an entire path in Jolt shiftr?

Suppose I want to transform the following
Original
{
"data": {
"a": {
"b": {
"c": {
"value": 1
}
}
}
}
}
For simplicity, say I just want to change value to newValue
Result
{
"data" : {
"a" : {
"b" : {
"c" : {
"newValue" : 1
}
}
}
}
}
I could do that with the following Jolt spec:
Spec
[
{
"operation": "shift",
"spec": {
"data": {
"a": {
"b": {
"c": {
"value": "&4.&3.&2.&1.newValue"
}
}
}
}
}
}
]
But I feel like there should be a less verbose syntax... perhaps something like the following (which does not work):
Desired Syntax... or something like it
[
{
"operation": "shift",
"spec": {
"data.a.b.c.value": "data.a.b.c.newValue" // Even nicer to use & somehow
}
}
]
Is there any Jolt shiftr functionality that I'm missing that would make this nicer?
There is not an existing transform that does that.
Couple of issues:
1) the transform you desire is more of a "nudge" than a "shift". In that you want to change a single value in the Json tree and leave the rest of the tree alone. That doesn't exist in Jolt.
"shift" makes a new output map, and "copies" data from the input to the output.
I tried making shift write to the same input Json tree structure, but it is "dangerous" as it can easily run into ConcurrentModification exceptions.
2) The transform style you have laid out is nice for simple things, but I don't know how it would scale to all the complex stuff wildcard logic (*, [], &).
It is the answer
{
"operation": "modify-overwrite-beta",
"spec": {
"jobInput": {
"ai": {
"common": {
"seed": "#(4,ai.model_structure.seed)"
}
}
}
}
}
#(4,ai.model_structure.seed)
upper sentence means backward 4step on json tree and use seed with this path(ai.model_structure.seed).