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.