โ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.