cancel
Showing results for 
Search instead for 
Did you mean: 

Converting an array to a JSON payload

jfpelletier
Contributor

Hello all,

I need to convert an array of ID values into a JSON payload, and I cannot find any array function that can iterate the values of the list to generate what I need.

My array of IDs looks like this: ["r4ige6rwgvnr", "4nkmdoijydfj9", "6ashbd99w3"]

The payload that I need is:

 

{
    "ids": [
        {
            "id": "r4ige6rwgvnr"
        },
        {
            "id": "4nkmdoijydfj9"
        },
        {
            "id": "6ashbd99w3"
        }
    ]
}

 

Is there a way (ideally built-in method) to iterate each ID from the array and generate the JSON format that I need?

Thanks a lot in advance!

JF

1 ACCEPTED SOLUTION

koryknick
Employee
Employee

@jfpelletier - assuming this input document:

{"ids":["r4ige6rwgvnr","4nkmdoijydfj9","6ashbd99w3"]}

Configure a Mapper expression as follows:

koryknick_0-1701881429942.png

Should provide the desired output.  Take a look at the Array.map() documentation for description.  The Javascript documentation linked in the SnapLogic documentation has some excellent details and examples.  Basically the statement shown above is turning each string value in the array to an object.

 

View solution in original post

3 REPLIES 3

koryknick
Employee
Employee

@jfpelletier - assuming this input document:

{"ids":["r4ige6rwgvnr","4nkmdoijydfj9","6ashbd99w3"]}

Configure a Mapper expression as follows:

koryknick_0-1701881429942.png

Should provide the desired output.  Take a look at the Array.map() documentation for description.  The Javascript documentation linked in the SnapLogic documentation has some excellent details and examples.  Basically the statement shown above is turning each string value in the array to an object.

 

Hello @koryknick,

Thanks a lot for this, it works exactly as you suggested! 🙂

Kind regards,

JF

clinton_hemmant
New Contributor III

Classic mapping! - Thanks @koryknick