javajsonjolt

Counter while transforming with Jolt


I am converting two array of objects into a single array of objects. While doing so, based on the type of the item , I need to assign a id for each item. And, after putting the items in a single array, the ids should be serial, in the order they originally appeared in the input json. its not single itemId for all the items. It s based on its own type. PFB the input and output JSON samples. Using jolt-core [0.1.0] . 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.


Solution

  • This (lengthy) spec should produce the output

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "Items": {
              "*": {
                "name": "@(1,type)"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&1[&0].name"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "&2[&1].&",
              "$1": "&2[&1].type"
            }
          }
        }
      },
      {
        "operation": "modify-default-beta",
        "spec": {
          "*": {
            "*": {
              "idName": "=concat(@(1,type), 'Id')"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "@": "&",
            "*": {
              "idName": {
                "$1": "&3[&2].idValue"
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "idValue": "=intSum(@(1,idValue), 1)"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "idValue": "&2[&1].@(1,idName)"
            },
            "@": "&"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "name|type|coldId|hotId": "&2[&1].&"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "items[]"
          }
        }
      }
    ]
    

    Try to examine the transformations' outputs one after another. Note: the order of the elements is different than in the desired output. If I find more time, I will try to sort it and (maybe) make it more concise.