06-14-2017 11:56 AM
I have a flat file shown here:
“firstName”: “Gregor”
“lastName”: “Samsa”
“mainAddress”: “123 Main St”
“mainCity”: “Boston”
“mainState”: “MA”
“mainZip”: “02131”
“mailingAddress”: “456 Mailing Ave”
“mailingCity”: “Somerville”
“mailingState”: “MA”
“mailingZip”: “02144”
“shippingAddress”: “789 Shipping Blvd”
“shippingCity”: “Brookline”
“shippingState”: “MA”
“shippingZip”: “02122”
I am trying to break this out into an array of address objects split up by type using a mapper snap. The only way I have been successful so far is to hard-code the index in, like this:
$mailingCity >= $addresses.address[0].city
$shippingAddress >= $addresses.address[1].city
In the documentation for the mapper snap, there is a section that I thought would solve my issue using (value.length) in the index position. However, when I use (value.length) directly, or try inputting my array:
($addresses.address.length)
the snap fails. Has anyone done something like this before? The target schema looks like this:
“first_name”: “Gregor”
“last_name”: “Samsa”
“addresses”: [
{“address_type”: “main”,
“street_line_1”: “123 Main St”,
“city”: “Boston”,
“state”: “MA”,
“zip”: “02131”
},
{“address_type”: “mailing”,
“street_line_1”: “456 Mailing Ave”,
“city”: “Somerville”,
“state”: “MA”,
“zip”: “02144”
},
{"address_type’: “shipping”,
“street_line_1”: “789 Shipping Blvd”,
“city”: “Brookline”,
“state”: “MA”,
“zip”: “02122”
}
]
Thanks.
06-14-2017 12:35 PM
I don’t think there’s a way to append in the Mapper. The problem, is that we cannot simply append an object for every row in the mapper. Starting with your example:
If we add a couple of other fields that need to be mapped:
The first row with ‘$mailingCity’ will add an element to the array. But, the second row should not add an element. It should modify the first one that was added. Then, the third row should go back to adding another element to the array.
If all the input documents always have the same set of fields, it might be easier to use the JSON-Generator. You can add an input view to the generator and then directly write the content, like so:
{
"addresses": [
{
"address_type": "main",
"street_line_1": $mainAddress,
"city": $mainCity
},
{
"address_type": "shipping",
...
}
]
}
So, every document that comes in will produce the JSON document with the given substitutions. Note that while you can reference document variables, the generator is a Velocity template and not the expression language. You can do basically the same thing expression language, I just suggest the generator because the text editor it provides might be easier to use.
You can also get fancier by using methods like mapValues() to split up the key and build the addresses list automatically based on they key names.
06-14-2017 12:55 PM
Cool, I didn’t realize the JSON generator could be used like that. I got around it by initializing a counter variable at 0 and then adding a new mapper for each possible type of address (and incrementing the counter on each mapper), but the JSON generator will probably be cleaner.
Thanks!
06-14-2017 05:14 PM
@jskrable ,You can do the same in mapper snap too using expression builder. Here is the screenshot.
Expression:
[{
“address_type”: “mailing”,
“street_line_1”: $mailingAddress,
“city”: $mailingCity,
“state”: $mailingState,
“zip”: $mailingZip
},
{
“address_type”: “main”,
“street_line_1”: $mainAddress,
“city”: $mainCity,
“state”: $mainState,
“zip”: $mainZip
},
{
“address_type”: “shipping”,
“street_line_1”: $shippingAddress,
“city”: $shippingCity,
“state”: $shippingState,
“zip”: $shippingZip
}]
Result:
06-20-2017 07:45 AM
Thanks! This was very helpful. Seems cleaner to do all this inside a mapper.