skip to Main Content

I have a JSON file with a fluctuating depth and each node (different depth). So it’s built like a General tree :

enter image description here

And here is a sample json (not the real one but a mock file but the structure remains the same) :

{
  "arbre" : {
    "children" : [
      {
        "id" : 1,
        "children" : [
          {
            "id" : 3,
            "children" : [
              {
                "id" : 6,
                "children" : []
              }
            ]
          },
          {
            "id" : 4,
            "children": []
          },
          {
            "id" : 5,
            "children": []
          }
        ]
      },
      {
        "id" : 2,
        "children" : [
          {
            "id" : 7,
            "children" : 
            [
              {
                "id" : 8,
                "children" : [
                  {
                    "id" : 9,
                    "children" : []
                  }
                ]
              },
              {
                "id" : 10,
                "children" : [
                  {
                    "id" : 11,
                    "children" : []
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}

I’m trying to build multiple list of id for each subtrees, so for exemple I need :

  • 1, 3, 6
  • 1, 4
  • 1, 5
  • 2, 7, 8, 9
  • 2, 7, 10, 11

I previously developped a routine that traverse the entire tree (recursive) using jackson and don’t know if I can adapt it to this use case.

Here it is :

/**
     * getJSONNode: not return value, recursive function that will browse (uses functionalities and objects of the com.fasterxml.jackson library)
     * 
     * 
     * {talendTypes} String, JsonNode
     * 
     * {Category} User Defined
     * 
     * {param} JsonNode(jNode) input: the function will check if the node is an array of object and will process and keep iterate until it reaches the bottom of the tree
     * {param} string(filepath) input: The string is a path to a directory where the temp files will be stored
     * 
     * {example} getJSONNode(jNode, "/test/") # Files will be generated in the directory /test/
     */
    public static void getJSONNode(JsonNode jNode, String filepath) throws IOException {
        //System.out.println("Get Node");
        JsonNode tempNoded = jNode.get("children");
        if(tempNoded.isArray()) {
            for(int i = 0; i < tempNoded.size(); i++) {
                System.out.println("Name : " + tempNoded.get(i).get("name"));
                System.out.println("Title : " + tempNoded.get(i).get("title"));
                System.out.println("Title : " + tempNoded.get(i).get("id"));
                System.out.println("================== Writing file ===================");
                String id = tempNoded.get(i).get("id").toString();
                String name = tempNoded.get(i).get("name").toString();
                String title = tempNoded.get(i).get("title").toString();
                String color = tempNoded.get(i).get("color").toString();
                String order = tempNoded.get(i).get("order").toString();
                String parentId = tempNoded.get(i).get("parentId").toString();
                String persons = tempNoded.get(i).get("persons").toString();
                String profils = tempNoded.get(i).get("profils").toString();
                String paramNoeud = tempNoded.get(i).get("paramNoeud").toString();
                String description = tempNoded.get(i).get("description").toString();
                // disable not present in all dimensions so not usable
                //String disabled = tempNoded.get(i).get("disabled").toString();
                String oldId = tempNoded.get(i).get("oldId").toString();
                
                String str = id + ";" + name + ";" + title + ";" + color + ";" + order + ";" + parentId + ";" + persons + ";" + profils + ";" + paramNoeud + ";" + description + ";" + oldId;
                byte[] strToBytes = str.getBytes();
                
                String filename = (name + "_" + order + ".csv").replaceAll(""", "");
                
                File file = new File(filepath + filename);
                file.getParentFile().mkdirs();
                file.createNewFile();               
                FileOutputStream outputStream = new FileOutputStream(filepath + filename);
                
                outputStream.write(strToBytes);
                outputStream.close();
                
                getJSONNode(tempNoded.get(i), filepath);
            }
            
        }
    }

So is there a method to build those lists and modify my routine to do it or something else (another routine, …)

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    With the solution in the (accpeted) answer (https://stackoverflow.com/a/76456855/14707253)

    I made a few modifications but the solution in itself worked so here is the final code : Function

    public List<List<String>> getSubtrees(JsonNode childrenNode, List<String> currentSubtree) {
            ArrayList<List<String>> subtrees = new ArrayList<>();
    
            if (childrenNode.size() == 0) {
                subtrees.add(currentSubtree);
            } else {
                for (int i = 0; i < childrenNode.size(); i++) {
                    //System.out.println(childrenNode.get(i).toString());
                    String id = childrenNode.get(i).get("id").toString();
                    //System.out.println(id);
                    JsonNode children = childrenNode.get(i).get("children");
                    List<String> childSubtree = new ArrayList<>(currentSubtree);
                    childSubtree.add(id);
                    //System.out.println(childSubtree.toString());
                    subtrees.addAll(getSubtrees(children, childSubtree));
                }
            }
            //System.out.println(subtrees.size());
            return subtrees;
        } 
    

    And the call of the function :

    public List<List<String>> preProcessGetSubtrees(String tree) throws JsonProcessingException, IOException {
            if(!isNullorEmpty(tree)) {
                List<String> currentSubtree = new ArrayList<>();
                ObjectMapper objMapper = new ObjectMapper();
                System.out.println("=========================== PLANETE TREE PROCESSING ========================");
                //List<List<String>> subtrees = 
                List<List<String>> subtrees = getSubtrees(objMapper.readTree(tree).get("arbre").get("children"), currentSubtree);
                System.out.println("=========================== PLANETE TREE PROCESSED ========================");
                return subtrees;
            } else {
                System.out.println("============================================================================");
                System.out.println("==================== WARNING THE TREE IS NOT VALID =========================");
                System.out.println("============================================================================");
                return null;
            }
    

  2. I would suggest a recursive solution that keeps track of current subtree:

    publi List<List<String>> getSubtrees(JsonNode childrenNode, List<String> currentSubtree) {
        ArrayList<List<String>> subtrees = new ArrayList<>();
    
        if (childrenNode.isEmpty()) {
            subtrees.add(currentSubtree);
        } else {
            for (JsonNode node : childrenNode) {
                String id = node.get("id").asText();
                JsonNode children = node.get("children");
                List<String> childSubtree = new ArrayList<>(currentSubtree);
                childSubtree.add(id);
    
                subtrees.addAll(getSubtrees(children, childSubtree));
            }
        }
    
        return subtrees;
    }
    

    call it as

    List<List<String>> subtrees = getSubtrees(children, List.of());
    

    where children is an array node of top level children, in your case it’s a arbre -> children json node. That’s an example, pay attention to type/null checks.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search