Search code examples
arraysjsonmappingsnaplogic

SnapLogic - Altering array elements in Mapper


I currently have an incoming .json file that I am routing through a Mapper snap. In that mapper snap, I need to take an array of names bob.jones, eric.smith, etc, and add the string @email.com to each of them. The path for these names in the schema is:

jsonPath($, "$response.entity[*].target.name")

And the structure looks like this:

{
    "entity": [
        {
            "target": {
                "name": "bob.jones"
            }
        },
        {
            "target": {
                "name": "eric.smith"
            }
        },
        ...
    ]
}

Just mapping the path jsonPath($, "$response.entity[*].target.name") will pass the names and output bob.jones and eric.smith. I tried to use .concat() on the array but all that did was add @email.com into the list so I got bob.jones, eric.smith, @email.com. I am trying to figure out an expression that can edit the array items without changing their structure.


Solution

  • Based on the JSON path provided by you - jsonPath($, "$response.entity[*].target.name") - I created the following JSON.

    {
        "response": {
            "entity": [
                {
                    "target": {
                        "name": "bob.jones"
                    }
                },
                {
                    "target": {
                        "name": "eric.smith"
                    }
                }
            ]
        }
    }
    

    And I put it in a JSON Generator so that I can get it as documents. Refer to the following screenshot.

    JSON Generator

    If your desired output is as follows:

    mapper case 1

    Then use the following expression in the mapper.

    $response.entity.map(e => e.target.name + '@email.com')
    

    Else if your desired output is as follows:

    mapper case 2

    Then use the following setting in your mapper.

    mapper case 2 setting