Created on 09-26-2022 07:26 PM - edited 09-26-2022 10:41 PM
If I have a flow file with the following JSON how can I
1) Evaluate "addresses" to determine if it is of type Array or type Map
2) If type Map then convert "addresses" into an array using native NiFi capabilities (i.e. no string parsing)?
{
"name": "John Doe",
"addresses": {
"work": {
"number": "123",
"street": "5th Avenue",
"city": "New York",
"state": "NY",
"zip": "10020"
}
}
}
This is what I need it to look like:
{
"name": "John Doe",
"addresses": [{
"work": {
"number": "123",
"street": "5th Avenue",
"city": "New York",
"state": "NY",
"zip": "10020"
}
}]
}
I appreciate the input and support! Thank you.
Created on 09-27-2022 09:16 AM - edited 09-27-2022 09:16 AM
Hi,
Is it always the case that when you have one address you will have a map and if its multiple addresses then its an array? If that is true, then can use the EvaluateJsonPath processor to get the size of child elements of the "addresses" key with the following json path expression:
$.addresses.length()
If you have one address then the result is 1 otherwise >1. Then you can use RouteOnAttribute when the length is 1 to Jolt Transformation processor with the following spec to convert to Array:
[
{
"operation": "shift",
"spec": {
"addresses": {
"*": "addresses.[].&"
},
"*": "&"
}
}
]
If that helps please accept solution.
Thanks
Created on 09-27-2022 09:16 AM - edited 09-27-2022 09:16 AM
Hi,
Is it always the case that when you have one address you will have a map and if its multiple addresses then its an array? If that is true, then can use the EvaluateJsonPath processor to get the size of child elements of the "addresses" key with the following json path expression:
$.addresses.length()
If you have one address then the result is 1 otherwise >1. Then you can use RouteOnAttribute when the length is 1 to Jolt Transformation processor with the following spec to convert to Array:
[
{
"operation": "shift",
"spec": {
"addresses": {
"*": "addresses.[].&"
},
"*": "&"
}
}
]
If that helps please accept solution.
Thanks
Created 09-27-2022 11:26 AM
Brilliant! Exactly what I was looking for. Although it seems a little peculiar to me that we need to rely on a Jolt transform for this operation and not the UpdateRecord processor. Particularly since NiFi makes it a point to discuss Arrays and Maps in the documentation.
Thanks for the Jolt transform because I spent a lot of time trying to get the Jolt transform to work and couldn't quite figure it out. Now I see what I was doing wrong.