Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

JoltTransformJSON - Extract attribute witouth remove it

avatar
Explorer

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

1 ACCEPTED SOLUTION

avatar
Master Guru

@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()}"
}

View solution in original post

4 REPLIES 4

avatar
Master Guru

@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()}"
}

avatar

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.

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.

avatar
Explorer

thanks!! it's works

avatar
Expert Contributor

Hi @calonsca!

Please have a look at this spec as well!

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