Java code to generate JSON hierarchical path -
i working on project need parse json in java , display hierarchical path of json key/node mentioned below.
i'm developing program in java using jackson api not able readily available api return me hierarchical path of current key/node json.
json data -
{“tomcat-users”: { “role”:[ {“@rolename”:”manager-gui”}, {“@rolename”:”manager-script”}, {“@rolename”:”manager-jmx”}, {“@rolename”:”manager-status”} ], ”user”:{ “@username”:”admin”, ”@roles”:”manager-gui,manager-script”, ”@password”:”admin” } } }
output should like -
column 1 column 2 --------------------------------------------------------------------------- tomcat-users -> role[1] -> @rolename manager-gui tomcat-users -> role[2] -> @rolename manager-script tomcat-users -> role[3] -> @rolename manager-jmx tomcat-users -> role[4] -> @rolename manager-status tomcat-users -> user -> @username admin tomcat-users -> user -> @roles manager-gui,manager-script tomcat-users -> user -> @password admin
can please tell me, api available return me path ?
my java code -
public static void generatejsonpath(jsonnode node) { iterator<string> fieldnames = node.getfieldnames(); while(fieldnames.hasnext()){ string fieldname = fieldnames.next(); jsonnode fieldvalue = node.get(fieldname); if (fieldvalue.isobject()) { system.out.println(fieldname + " -> "); generatejsonpath(fieldvalue); } else if(fieldvalue.isarray()) { system.out.println(fieldname + " -> "); iterator<?> iterator = fieldvalue.getelements(); while(iterator.hasnext()) { generatejsonpath((jsonnode) iterator.next()); } } else { string value = fieldvalue.astext(); system.out.println(fieldname + " : " + value); } } }
java code result -
tomcat-users -> role -> @rolename : manager-gui @rolename : manager-script @rolename : manager-jmx @rolename : manager-status user -> @username : root @password : root @roles : manager-gui,manager-script,manager-jmx,manager-status