0

At present I am pulling my folder structure data as mentioned below.

{
{
"parent": "TestParent1"
"Child" : "TestSub1"
"GrnadChild":"TestGrandChild1"
}, 
{
  "parent": "TestParent1"
  "Child" : "TestSub1"
  "GrnadChild":"TestGrandChild2"
},
{
  "parent": "TestParent1"
  "Child" : "TestSub2"
  "GrnadChild":"TestGrandChild3"
  },
{
 "parent": "TestParent1"
 "Child" : "TestSub2"
 "GrnadChild":"TestGrandChild4"
}
}

I have a parent folder that consists 2 sub folder and each sub folder consists 2 grand folders. I need the data to be framed in below json format.

 {
  "parent": "TestParent1",
  {
   "Child" : "TestSub1"
     {
      "GrnadChild" :
       {
        "TestGrandChild1",
        "TestGrandChild2"
       }
     },
   "Child" : "TestSub2"
     {
      "GrnadChild" :
      {
        "TestGrandChild3",
        "TestGrandChild4"
      }
    }
 },
"parent": "TestParent2",
 {
  "Child" : "TestSub3"
   {
    "GrnadChild" :
     {
       "TestGrandChild5",                                                              
       "TestGrandChild6"
     }
   },
   "Child" : "TestSub4"
   {
    "GrnadChild" :
    {
     "TestGrandChild7",                                                              
     "TestGrandChild8"
    }
   }
  }
  }

Kindly assist me to wirte the java logic and Appreciate your help.

RKN
  • 21
  • 4
  • Duplicate: https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java ...maybe others – gagarwa Aug 03 '20 at 13:51
  • There are many Java libraries for parsing JSON: Jackson, GSON, org.json. Pick one, read a tutorial, and come back if you have more questions. – gagarwa Aug 03 '20 at 13:53
  • Hi gagarwa,It seems the url you provided is different than what i am asking for. i need to display the data in tree format. Thank you for the reply anyhow. – RKN Aug 03 '20 at 14:08
  • You want to reformat the input...the libraries used are the same. If you are ok serializing the data into a data model, you can use Jackson. Then you can convert to your desired model, and output using Jackson. To modify, use Jackson Annotations. I will add this as an answer. – gagarwa Aug 03 '20 at 14:24

2 Answers2

0
  1. Serialize JSON to Object

    String json;
    FolderStruc original = new ObjectMapper()
       .readerFor(FolderStruc.class)
       .readValue(json);
    
  2. Convert to Desired Object Model

    DesiredStruc desired = new DesiredStruc(original);
    
  3. De-serialize Object to JSON

    String json = new ObjectMapper().writeValueAsString(desired);
    

If you are having any issues with correctly mapping your model to the desired output, try these annotations:

https://www.baeldung.com/jackson-annotations

gagarwa
  • 1,426
  • 1
  • 15
  • 28
0

Here is a code for implementation. You have to add json-simple jar to your library

    JSONArray grandchild = new JSONArray();
    JSONArray child = new JSONArray();
    JSONArray parent = new JSONArray();
    
    grandchild.add("GC1");
    grandchild.add("GC1");
    
    child.add("C1");
    JSONObject obj = new JSONObject();
    obj.put("GC", grandchild);
    child.add(obj);
    
    parent.add("P");
    obj = new JSONObject();
    obj.put("C", child);
    parent.add(obj);
    

    System.out.println(parent);

Output generated will look like :

["P",{"C":["C1",{"GC":["GC1","GC1"]}]}]
bhuwan
  • 116
  • 5
  • Hi Bhuvan, Thank you. But i need a logic to add parent and sub folders and grand folders based on the conditions. The data i given is just sample which is coming from DB. – RKN Aug 03 '20 at 15:36