Skip to main content
ChuckE
Gold ⭐️
September 27, 2022
Solved

Convert a Map Field into an Array

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.

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
September 27, 2022

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

 

 

2 replies

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
September 27, 2022

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

 

 

Translate
ChuckE
ChuckEAuthor
Gold ⭐️
September 27, 2022

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.  

Translate