Support Questions

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

Convert a Map Field into an Array

avatar
Expert Contributor

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.

1 ACCEPTED SOLUTION

avatar

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

 

 

View solution in original post

2 REPLIES 2

avatar

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

 

 

avatar
Expert Contributor

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.