Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

java - 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.

question from:https://stackoverflow.com/questions/65921966/counter-while-transforming-with-jolt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...