I have a JSON file with a fluctuating depth and each node (different depth). So it’s built like a General tree :
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
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
And the call of the function :
I would suggest a recursive solution that keeps track of current subtree:
call it as
where
children
is an array node of top level children, in your case it’s aarbre -> children
json node. That’s an example, pay attention to type/null checks.