Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

NiFi JoltTransformJSON 2.0.0 trasform JSON flowfile

avatar
Frequent Visitor

Hi Everyone,

Struggling with transforming the JSON flow file, would appreciate help from the community!

input json flow file:

[{
"firstName": "John",
"LastName": "Malkovich",
"MiddleName": "J",
"nameType": {
"type1": "Other"
"type2": "Primary"
}
}, {
"firstName": "Johnny",
"LastName": "Cage",
"MiddleName": "M",
"nameType": {
"type1": "Basic"
"type2": "Primary"
}
}
]

expected output json flow file:

{
"fileName": "file.xml",
"count": "2",
"individuals": [{
"firstName": "John",
"LastName": "Malkovich",
"MiddleName": "J",
"nameType": {
"type1": "Other"
"type2": "Primary"
}
}, {
"firstName": "Johnny",
"LastName": "Cage",
"MiddleName": "M",
"nameType": {
"type1": "Basic"
"type2": "Primary"
}
}
]​
}

Jolt  specification :
[ { "operation": "default", "spec": { "fileName": "file.xml"", "count": "=2" } }, { "operation": "shift", "spec": { "*": { "@": "individuals[]" } } } ]

 

gettingError:

TransformException: The Spec provided can not handle input that is a top level Json Array.

 

1 ACCEPTED SOLUTION

avatar
Super Guru

Hi @FreddyM ,

It seems like the default spec doesnt like it when the root object is an array. The good news is that there are other ways to assign default values , for example in the shift spec using # on the left hand side allows you to set default as follows:

[
  {
    "operation": "shift",
    "spec": {
      "#file.xml": "fileName",
      "#2": "count",
      "*": {
        "@": "individuals[]"
      }
    }
	}
]

If you have the fileName and the count values stored in flow file attribute you can reference them using expression language as in :

"#${fileNameAttribute}":"fileName"

Hope that helps. Please accept solution if it does.

Thanks

View solution in original post

2 REPLIES 2

avatar
Super Guru

Hi @FreddyM ,

It seems like the default spec doesnt like it when the root object is an array. The good news is that there are other ways to assign default values , for example in the shift spec using # on the left hand side allows you to set default as follows:

[
  {
    "operation": "shift",
    "spec": {
      "#file.xml": "fileName",
      "#2": "count",
      "*": {
        "@": "individuals[]"
      }
    }
	}
]

If you have the fileName and the count values stored in flow file attribute you can reference them using expression language as in :

"#${fileNameAttribute}":"fileName"

Hope that helps. Please accept solution if it does.

Thanks

avatar
Frequent Visitor

@SAMSAL  Solution worked like a charm, special thank you for additional hint on passing attribute to json payload. 
Thank you.