Forum Discussion
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.
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!
- psadasivam9 years agoNew Contributor III
@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:
- jskrable9 years agoNew Contributor III
Thanks! This was very helpful. Seems cleaner to do all this inside a mapper.