Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4025030
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:47:44+00:00 2026-05-20T10:47:44+00:00

I write an application which creates a JSON File following the below structure: {

  • 0

I write an application which creates a JSON File following the below structure:

{
  identifier:"id",
label:"name",
items:[
  {
     id:"ROOT",
     name:"Parent1",
     type:"ROOT",
     children:[
        {
           _reference:"CHILD1"
        }
     ]
  },
  {
     id:"CHILD1",
     name:"Parent1-Child1",
     type:"Child",
     children:[
        {
           _reference:"CHILD2"
        }
     ]
  },
  {
     id:"CHILD2",
     name:"Child1-Child2",
     type:"Child",
     children:[
        {
           _reference:"Child3"
        },
        {
           _reference:"Child4"
        }
     ]
  },
  {
     id:"Child3",
     name:"Child2-Child3",
     type:"GrandChild"
  },
  {
     id:"Child4",
     name:"Child2-Child4",
     type:"GrandChild"
  },

]
}

So my application actually gets following data for each node of this JSON Tree

PID– It to which i have to attach the child node
CID– ID of Child Node
CNAME– Child Node Name
CTYPE– Child Type

So currently what i am doing is searching the PID in the json file through iterating ( on file system) and once i find the String which has id=PID,i append a Child Ref to that Node

    {
       _reference:"CHILD-NEXT"
    }

and then after this line adding the whole new child node

{
 id:"CHILD-NEXT",
 name:"Parent1-ChildNext",
 type:"Child",
},

But this is very inefficient solution because of iterating the file every time to search the parent node and appending/inserting the child.

So i am looking for a kind of solution in which i just create this JSON structure in memory and when i am done with adding all the nodes then i can write it to the file. I am doing this in JAVA,so i am looking for some existing JSON Libraries which i can use to fulfil this requirement.

Please comment if you need any other information of the above explanation is not clear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:47:45+00:00Added an answer on May 20, 2026 at 10:47 am

    You can use the org.json library to do this.

    You first need to read your json file into a JSONObject which is basically made up of key/value pairs and arrays. You can then loop over the items in the object, look for a pid and add a new child.

    Here is some sample code to get you started:

    //read file        
    BufferedReader in = new BufferedReader(new FileReader("path/json.txt"));
    String line;
    StringBuilder sb = new StringBuilder();
    while((line =in.readLine()) != null){
        sb.append(line);
    }
    in.close();
    
    
    //create a json object
    JSONObject json = new JSONObject(sb.toString());
    
    //pid to search for
    String pid = "XXX";
    
    //loop over the items array and look for pid
    JSONArray items = json.getJSONArray("items");
    for(int i = 0 ; i < items.length(); i++){
        JSONObject item = items.getJSONObject(i);
        String id = item.getString("id");
        if(pid.equals(id)){
    
            //get the children
            JSONArray children;
            if(item.has("children")){
                children = item.getJSONArray("children");    
            }
            else{
                children = new JSONArray();
                item.put("children", children);
            }
    
            //append a new child ref to the children
            JSONObject ref = new JSONObject();
            ref.put("_reference", "CHILD-NEXT");
            children.put(ref);
    
            //create a new child node
            JSONObject newItem = new JSONObject();
            newItem.put("id", "CHILD-NEXT");
            newItem.put("name", "Parent1-ChildNext");
            newItem.put("type", "Child");
            items.put(newItem);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a small wsgi application which will put some objects to
I'm planning to write gateway web application, which would need terminal window with VT100/ANSI
I wrote an application recently, which relies on OpenID for authentication. A lot of
Question: Should I write my application to directly access a database Image Repository or
I am attempting to write an application that uses libCurl to post soap requests
I'm trying to write an application that converts 48 bit-per-pixel PNG files to a
I would like to write an application that will copy MP3 files to a
I am just beginning to write an application. Part of what it needs to
I cannot figure out how to make a C# Windows Form application write to
When using vxWorks as a development platform, we can't write our application with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.