Support Questions

Find answers, ask questions, and share your expertise

Convert a Map Field into an Array

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

Super Collaborator

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

Super Collaborator

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

 

 

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.  

Take a Tour of the Community
Don't have an account?
Your experience may be limited. Sign in to explore more.