Skip to main content
calonsca
Silver ⭐️
Jun 17, 2019 6:23 PM UTC
Solved

JoltTransformJSON - Extract attribute witouth remove it

Hello everyone,


first at all, i am kind of newbie with nifi so feel free to change everywhting you see.


I am ussing JoltTransformJSON. My input JSON is the next one:


{

"ID": "123",

"Text1": "aaa",

"Text2": "aaa",

"Text3": "aaa"

}


And i need the next output:


{

"data": {

"ID": "123",

"Text1": "aaa",

"Text2": "aaa",

"Text3": "aaa"

},

"date": "",

"dataset": "",

"ID": "123"

}


The var date and dataset are attributes from the flow, so there is no problem here, the problem is i need to extract the value of ID, in this case 123 but this is just an example, without deleted the ID inside de field data. My Jolt Specification is the next one:


[{

"operation": "shift",

"spec": {

"*": "data.&"

}

}, {

"operation": "default",

"spec": {

"dataset": "${dataset:toLower()}",

"date": "${date}"

}

}]


and with that i have all EXCEPT the ID field, that i dont know how to do it :(.


Thankss

Shu_ashu
Shu_ashuAnswer
Gold ⭐️⭐️⭐️
Jun 18, 2019 7:58 AM UTC

@Carlos

Try with below spec:

[{
"operation": "shift",
"spec": {
"*": "data.&",
"ID": ["ID", "data.ID"]
}
}, {
"operation": "default",
"spec": {
"dataset": "${dataset:toLower()}",
"date": "${date}"
}
}]

Output:

{
"ID" : "123",
"data" : {
"ID" : "123",
"Text1" : "aaa",
"Text2" : "aaa",
"Text3" : "aaa"
},
"date" : "${date}",
"dataset" : "${dataset:toLower()}"
}

4 replies

Shu_ashu
Shu_ashuAnswer
Gold ⭐️⭐️⭐️
Jun 18, 2019 7:58 AM UTC

@Carlos

Try with below spec:

[{
"operation": "shift",
"spec": {
"*": "data.&",
"ID": ["ID", "data.ID"]
}
}, {
"operation": "default",
"spec": {
"dataset": "${dataset:toLower()}",
"date": "${date}"
}
}]

Output:

{
"ID" : "123",
"data" : {
"ID" : "123",
"Text1" : "aaa",
"Text2" : "aaa",
"Text3" : "aaa"
},
"date" : "${date}",
"dataset" : "${dataset:toLower()}"
}
Translate
ask_bill_brooks
Platinum ⭐️
Platinum ⭐️
Jun 18, 2019 10:09 AM UTC

The above was originally posted in the Community Help Track. On Tue Jun 18 03:07 UTC 2019, a member of the HCC moderation staff moved it to the Data Ingestion & Streaming track. The Community Help Track is intended for questions about using the HCC site itself.

Translate
Bill Brooks, Community Moderator Was your question answered? Make sure to mark the answer as the accepted solution. If you find a reply useful, say thanks by clicking on the thumbs up button.
calonsca
calonscaAuthor
Silver ⭐️
Jun 18, 2019 12:59 PM UTC

thanks!! it's works

Translate
PVVK
Gold ⭐️
Sep 30, 2020 7:47 PM UTC

Hi @calonsca!

Please have a look at this spec as well!

[
{
"operation": "shift",
"spec": {
"@": "data",
"ID": "&",
"#${date}": "date",
"#${dataset:toLower()}": "dataset"
}
}
]

Translate