Forum Discussion

ruchitvijay's avatar
ruchitvijay
New Contributor
6 years ago

Mapping requirement

I have the following :-
[{
“key”:[
“name”,
“organization”,
“city”,
“state”
],
“value”:[
“abc”,
“xyz”,
“pune”,
“maharashtra”
]},
{
“key”:[
“name”,
“organization”,
“city”,
“state”
],
“value”:[
“cde”,
“pqr”,
“mumbai”,
“maharashtra”
]},
{
“key”:[
“name”
“city”
“country”
],
“value”:[
“def”
“nashik”
“india”
]
}]

how do i convert this into key:value set to write this into csv
Requirement-
{
“name”:{“abc”, “cde”, “def”},
“organization”:{“xyz”, “pqr”, “null”},
“city”:{“pune”, “mumbai”, “nashik”},
country:{“null”, “null”, “india”}
}

2 Replies

  • @ruchitvijay

    Is this:

    {
    “name”:{“abc”, “cde”, “def”},
    “organization”:{“xyz”, “pqr”, “null”},
    “city”:{“pune”, “mumbai”, “nashik”},
    country:{“null”, “null”, “india”}
    }

    really your desired data? These objects are invalid since there is not key/value pair. Instead it seems those should be lists like this:

    {
    “name”:[“abc”, “cde”, “def”],
    “organization”:[“xyz”, “pqr”],
    “city”:[“pune”, “mumbai”, “nashik”],
    country:[“india”]
    }

    assuming that you are okay with lists, I’ve attached an example pipeline with a json generator mocking your given data, and a mapper that produces the results given above.

    Basic idea to the approach taken was to zip the key/value pair together, then to reduce the array of pairs into the desired document with a simple ternary operator to see if a concat operation was needed or this was the first time this key was seen then insert a new array.

    CommunityExpression_2019_10_21.slp (4.3 KB)