cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Create new object at end of array

jskrable
New Contributor III

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.

4 REPLIES 4

tstack
Former Employee

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.

jskrable
New Contributor III

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!

psadasivam
New Contributor III

@jskrable ,You can do the same in mapper snap too using expression builder. Here is the screenshot.

ce7c69bd7fa6e264d614ee3dc95affd0ecf5a7fa.png

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:
c32a8c993157b6f625ac40b3a207c2aefb777aa5.png

jskrable
New Contributor III

Thanks! This was very helpful. Seems cleaner to do all this inside a mapper.