bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 329 forks source link

counter while transforming | JOLT #1055

Open san29-commits opened 3 years ago

san29-commits commented 3 years ago

Hi, I am in need to add a id to a output json while converting array of array into a single array. Please help.

Input Json:

{
    "Beverages" : {
        "Items" : [ {
            "name" : "cofee",
            "type" : "hot"
        },{
            "name" : "wine",
            "type" : "cold"
        },{
            "name" : "tea",
            "type" : "hot"
        },{
            "name" : "beer",
            "type" : "cold"
        }
        ]   
    },
    "Snacks" : {
        "Items" : [ {
            "name" : "crackers",
            "type" : "hot"
        },{
            "name" : "salad",
            "type" : "cold"
        },{
            "name" : "muffin",
            "type" : "cold"
        },{
            "name" : "toast",
            "type" : "hot"
        }
        ]
    }
}

And, the expected output is:

{
    "items" : [{
            "name" : "cofee",
            "type" : "hot",
            "hotId" : 1
        },{
            "name" : "wine",
            "type" : "cold",
            "coldId" : 1
        },{
            "name" : "tea",
            "type" : "hot",
            "hotId" : 2
        },{
            "name" : "beer",
            "type" : "cold",
            "coldId" : 2
        },{
            "name" : "crackers",
            "type" : "hot",
            "hotId" : 3
        },{
            "name" : "salad",
            "type" : "cold",
            "coldId" : 3
        },{
            "name" : "muffin",
            "type" : "cold",
            "coldId" : 4
        },{
            "name" : "toast",
            "type" : "hot",
            "hotId" : 4
        }

    ]
}

As above, the hotId and coldId are the new fields in the output. But, it increments.

san29-commits commented 3 years ago

@milosimpson , please help