Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- groovy.grape.Grape.grab([group: 'com.fasterxml.jackson.core', artifact: 'jackson-databind', version: '2.9.9'])
- om = new com.fasterxml.jackson.databind.ObjectMapper();om.enable(com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT);
- remap = { map-> levels = map.keySet().grep{ it.startsWith("level") }; if(levels.size()>1) {throw new RuntimeException(levels) }; levels.each{ map.put("children", map[it]); map.remove(it); }; map["children"].each{ remap(it) } };
- koatuu = om.readValue(new File(System.getProperty("user.home")+"/Downloads/koatuu.json").newInputStream(), TreeMap.class); 0
- remap(koatuu); 0
- restruct = { map ->
- if(map["children"] !=null && map["children"].size()>0) {
- codes = map["children"].collect{ it["code"].replaceAll("0+\$", "") }.sort{ it.length() };
- rootCode = codes.size()>0?codes.first():null;
- if(rootCode!=null) {
- rootNode = map["children"].grep{ it["code"].replaceAll("0+\$", "") == rootCode }.first();
- map["children"].grep{ it["code"].startsWith(rootCode) }.each{
- if(it != rootNode) {
- println "Restructuring "+it["code"]+" under root code "+rootNode["code"];
- if(rootNode["children"] == null) {
- rootNode["children"] = new LinkedList();
- }
- rootNode["children"].add(it);
- map["children"].remove(it);
- }
- }
- map["children"].each{ restruct(it); }
- }
- }
- }
- restruct(koatuu); 0
- new File(System.getProperty("user.home")+"/Downloads/koatuu_fix2.json").text = om.writerWithDefaultPrettyPrinter().writeValueAsString(koatuu); 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement