Skip to main content
nict
Bronze ⭐️⭐️⭐️
July 31, 2023
Solved

olt transform

I am a newbie to Nifi Jolt transform.  I've been looking through the document, but still cannot figure out how to do mutiple headings for one array.

input json

{
"dataPackage": {
"datatest": {
"field": [
{
"Name": "test1",
"content": 45
},
{
"Name": "test2",
"content": 20
},
{
"Name": "test3",
"content": 23
}
]
}
}
}

Jolt Spec

[
{
"operation": "shift",
"spec": {
"dataPackage": {
"datatest": {
"field": {
"*": {
"Name": {
"test1": {
"#test1": "field[&3].Name",
"@(2,content)": "field[&3].content"
},
"test2": {
"#test2": "field[&3].Name",
"@(2,content)": "field[&3].content"
},
"test3": {
"#test3": "field[&3].Name",
"@(2,content)": "field[&3].content"
}
}
}
}
}
}
}
}, {
"operation": "shift",
"spec": {
"field": {
"*": {
"@(0,content)": "firstDetails.@(1,Name)"
}
}
}
}
]

current output

{
"firstDetails" : {
"test1" : 45,
"test2" : 20,
"test3" : 23
}
}

Expected output

{
"firstDetails" : {
"test1" : 45,
"test2" : 20
},
"secondDetails" : {
"test3" : 23
}
}

can you please help

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
July 31, 2023

Hi @nict ,

Not sure how you want to add your headings (firstDetails, secondDetails...etc.) specially secondDetails is not found anywhere in your jolt spec. If you are just looking to associate different records (test1, test2, test3...etc.) to different header based on the value of "Name" where you have to list each Name value explicitly,  then something like this will work:

[
{
"operation": "shift",
"spec": {
"dataPackage": {
"datatest": {
"field": {
"*": {
"Name": {
"test1": {
"@(2,content)": "firstDetails.test1"
},
"test2": {
"@(2,content)": "firstDetails.test2"
},
"test3": {
"@(2,content)": "secondDetails.test3"
}
}
}
}
}
}
}
}
]

You can add more values\header as needed. However, if you are looking to create header dynamically based on the Name values then you have to provide more info on how you would associate records with header.

 

If that helps please accept solution.

Thanks

 

1 reply

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
July 31, 2023

Hi @nict ,

Not sure how you want to add your headings (firstDetails, secondDetails...etc.) specially secondDetails is not found anywhere in your jolt spec. If you are just looking to associate different records (test1, test2, test3...etc.) to different header based on the value of "Name" where you have to list each Name value explicitly,  then something like this will work:

[
{
"operation": "shift",
"spec": {
"dataPackage": {
"datatest": {
"field": {
"*": {
"Name": {
"test1": {
"@(2,content)": "firstDetails.test1"
},
"test2": {
"@(2,content)": "firstDetails.test2"
},
"test3": {
"@(2,content)": "secondDetails.test3"
}
}
}
}
}
}
}
}
]

You can add more values\header as needed. However, if you are looking to create header dynamically based on the Name values then you have to provide more info on how you would associate records with header.

 

If that helps please accept solution.

Thanks

 

Translate