Getting array length in json, Nifi - json

I have the following input:
{
"abc": [1,2,3,4,5],
"ghf": [2,4,6,8]
}
In the desired output I wanted to have the array length of these fields.
{
"abc" : 5,
"ghf" : 4
}
Which processors should I be using and how?
Thanks in advance.

You can use modify-overwrite-beta transformation along with a list function called size within a JoltTransformJSON processor such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=size(#(1,&))"
}
}
]

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

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

Break down JSON properties to array of objects

I am trying to transform a simple JSon object into an array of objects with keys and values broken out, but I'm not sure how to quite get there.
I have tried this a number of ways but the closest I got was to create an object with two arrays, instead of an array with multiple objects with two properties each:
EDIT: I am trying to write a spec which would take any object, not this specific object. I do not know what the incoming object will be other than it will have simple properties (values will not be arrays or other objects).
Sample Input:
{
"property": "someValue",
"propertyName" : "anotherValue"
}
Expected Output:
{
"split_attributes": [
{
"key" : "property",
"value": "someValue"
},
{
"key" : "propertyName",
"value" : "anotherValue"
}
]
}
My spec so far:
{
"operation": "shift",
"spec": {
"*": {
"$": "split_attributes[#0].key",
"#": "split_attributes[#0].value"
}
}
}
Produces
{
"split_attributes" : [
{
"key" : [ "property", "propertyName" ],
"value" : [ "someValue", "anotherValue"]
}
]
}
SOLUTION
I was pretty close, and after looking at the tests, the solution was obvious (it's identical to one of the tests)
{
"operation": "shift",
"spec": {
"*": {
"$": "split_attributes[#2].key",
"#": "split_attributes[#2].value"
}
}
}
From what it seems, I was creating an array but I was looking at the wrong level for an index to the new array. I'm still fuzzy on the whole # level (for example where in the "tree" (and of which object) is #0, #1 and #2 actually looking).