I have a csv file and wanted to form JSON objects by grouping using mutiple column of csv in java -
> csv looks : (,) sperated values
col_1 col_2 col_5 col_6 1 yyy1 1111 cdx00001 1 yyy2 1111 cdx00001 2 yyy3 3333 cdx00002 2 yyy4 2222 cdx00001 2 yyy5 3333 cdx00002 2 yyy6 3333 cdx00002 2 yyy7 3333 cdx00002 2 yyy8 4444 cdx00002 2 yyy9 4444 cdx00002
> **i used below code group 2 columns , json result looks below .
"cdx00002$@4444": [ { "col_5": "4444", "col_6": "cdx00002", "col_1": "2", "col_2": "yyy8" }, { "col_5": "4444", "col_6": "cdx00002", "col_1": "2", "col_2": "yyy9" } ], "cdx00002$@3333": [ { "col_5": "3333", "col_6": "cdx00002", "col_1": "2", "col_2": "yyy3" }, { "col_5": "3333", "col_6": "cdx00002", "col_1": "2", "col_2": "yyy5" }, { "col_5": "3333", "col_6": "cdx00002", "col_1": "2", "col_2": "yyy6" }, { "col_5": "3333", "col_6": "cdx00002", "col_1": "2", "col_2": "yyy7" } ], "cdx00001$@1111": [ { "col_5": "1111", "col_6": "cdx00001", "col_1": "1", "col_2": "yyy1" }, { "col_5": "1111", "col_6": "cdx00001", "col_1": "1", "col_2": "yyy2" }, { "col_5": "1111", "col_6": "cdx00001", "col_1": "2", "col_2": "yyy10" } ], "cdx00001$@2222": [ { "col_5": "2222", "col_6": "cdx00001", "col_1": "2", "col_2": "yyy4" } ] }
> code used :
map namesbycompany = list.stream() .collect(collectors.groupingby(item->item.get("col_6") +"$@"+item.get("col_5"),collectors.tolist())); gson gson = new gsonbuilder().setprettyprinting().create(); system.out.println("--result---"+gson.tojson(namesbycompany));
=========================================================
details :
but problem here groupby columns dynamic , cannot hard coded above, if 1 columns json should prepared considering new groupby well. need prepare json csv somehow using dynamic gorupby columns. tried in java if other tools or coding please post here . in advance
if every row 1 group, can make add groups json of group until doesn't find ',' .
Comments
Post a Comment