Extract the value from an array in Apache NiFi - json

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

Related

JSON object to array with JOLT

I have the following JSON object
Input:
{
"17.39.108.85:80": [],
"10.204.32.9:443": [
{
"status": "DOWN"
}
]
}
and trying to transform it into a list/array as below :
Desired Output:
[
{
"17.39.108.85:80": []
},
{
"10.204.32.9:443": [
{
"status": "DOWN"
}
]
}
]
What's the best way to use Jolt.
You can use this spec:
Currently, your input is correct, but you need to put each key into an array.
You can get each key in the object and send it to an array with [#2].
Note: #2 is the index of each key. for example: 17.39.108.85:80 is 0 and 10.204.32.9:443 is 1.
[
{
"operation": "shift",
"spec": {
"*": {
"#": "[#2].&"
}
}
}
]

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

Getting array length in json, Nifi

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

JSON attribute value split by space and put them into new attributes using Jolt transform Apache nifi

I have json object as following,
{
"sensorId":2,
"dataValue":26.7,
"dateTime":"2020:12:29 14:20:31"
}
I want to convert it to the following,
{
"sensorId":2,
"dataValue":26.7,
"date":"2020:12:29",
"time":"14:20:31"
}
Using Apache nifi Jolt transform
you can split with space ("* *") and assign splitted parts.
[
{
"operation": "shift",
"spec": {
"sensorId": "sensorId",
"dataValue": "dataValue",
"dateTime": {
"* *": {
"$(0,1)": "date",
"$(0,2)": "time"
}
}
}
}
]

How can I combine two arrays to create a key value pair with Jolt?

I've already created a spec to convert my JSON input
{
"rows": [
{
"row": [
"row1",
"row2",
"row3"
],
"header": [
"header1",
"header2",
"header3"
]
},
{
"row": [
"row4",
"row5",
"row6"
],
"header": [
"header4",
"header5",
"header6"
]
}
]
}
to convert to key-value pairs as following object result :
{
"header1" : "row1",
"header2" : "row2",
"header3" : "row3",
"header4" : "row4",
"header5" : "row5",
"header6" : "row6"
}
Is this possible to do using Jolt?
Is there a copy/paste error in your input? Judging by your desired output, the second object's header array should be ["header4", "header5", "header6"]. If that's the case, this spec should work:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"header": {
"*": {
"*": {
"#(3,row[#2])": "&"
}
}
}
}
}
}
}
]
One option is to use the following shift transformation spec :
[
{
"operation": "shift",
"spec": {
"*s": { // rows array
"*": {
"&(1,1)": { // row array
"*": {
"#": "#(3,header[&1])"
}
}
}
}
}
}
]
where
"*s": { stands for rows
"&(1,1)": { -> not immediate(zeroth) level but one more level up by using &(1, and grab the value there the first asterisk exists by &(..,1)
"#": "#(3,header[&1])" -> 3 levels needed as stated at the right hand side traverse the colon
as well in order to reach the level of &(1,1) which is used to
represent the "row" array along with &1 representation to go one level up the tree to reach the indexes of the array "row" while matching with the values of "row" through use of # on the left hand side
the demo on the site http://jolt-demo.appspot.com/ is :