cancel
Showing results for 
Search instead for 
Did you mean: 

Formatting variable JSON

maddog0
New Contributor II

I am working on an integration between two endpoints that deal with values in very different ways and I’m struggling to convert between them. The source I’m getting data from sends out data with mutliple values all on the same element so my input document looks like this

[
    {
        "Key": "V1,V2,V3"
    },
    {
        "Key": "V1"
    },
    {
        "Key": "V1"
    },
    {
        "Key": "V1,V2,V3,V4"
    }
]

but my output document needs to look like this

[
    {
        "keys": [
            {
                "key": {
                    "id": "V1"
                }
            },
            {
                "key": {
                    "id": "V2"
                }
            },
            {
                "key": {
                    "id": "V3"
                }
            }
        ]
    },
    {
        "keys": [
            {
                "key": {
                    "id": "V1"
                }
            }
        ]
    },
    {
        "keys": [
            {
                "key": {
                    "id": "V1"
                }
            }
        ]
    },
    {
        "keys": [
            {
                "key": {
                    "id": "V1"
                }
            },
            {
                "key": {
                    "id": "V2"
                }
            },
            {
                "key": {
                    "id": "V3"
                }
            },
            {
                "key": {
                    "id": "V4"
                }
            }
        ]
    }
]

Does anyone have suggestions for a good way to deal with this conversion? Due to the variable length of each document I’m thinking I might need to use a child pipeline so I can build the output JSON element by element, but I’m not sure if that’s the best way to go about this

1 ACCEPTED SOLUTION

maddog0
New Contributor II

Thanks for the help! That didn’t get me all the way there, but after looking at it I realized I could use another map to get the values I needed. In the end I did this

$Key.split(',').map(x=>{"id":x}).map(x=>{"key":x})

which got me the exact output I was looking for

View solution in original post

2 REPLIES 2

bojanvelevski
Valued Contributor

Hi @maddog0,

Try and map the following expression in a Mapper:

{"key":$Key.split(',').map(x=>{"id":x})}

With target path keys

image

Let us know if this helps.
Regards,
Bojan

maddog0
New Contributor II

Thanks for the help! That didn’t get me all the way there, but after looking at it I realized I could use another map to get the values I needed. In the end I did this

$Key.split(',').map(x=>{"id":x}).map(x=>{"key":x})

which got me the exact output I was looking for