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

  • Home
  • SEARCH
  • 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 1078761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:46:15+00:00 2026-05-16T21:46:15+00:00

I have a JSON tree that contains nodes and children – the format is:

  • 0

I have a JSON tree that contains nodes and children – the format is:

jsonObject = 
{
  id:nodeid_1,
  children: [
  {
    id:nodeid_2,
    children:[]
  },
  {
    id:nodeid_3,
    children:[
    {
      id:nodeid_4,
      children:[]
    },
    {
      id:nodeid_5,
      children:[]
    }
  }
}

I don’t know the depth of this tree, a node is capable of having many children that also have many children and so on.

My problem is that I need to add nodes into this tree by using a nodeID. For example, a function that can take a nodeID and the node object (including its children), would be able to replace that node within the tree – which as a result would become a bigger tree.

I have only come across recursive functions that allow me to traverse all the nodes within a JSON tree and a modification I have made of one of these functions returns me the node object – but doesn’t help me as I need to modify the original tree:

var findNode = {
node:{},
find:function(nodeID,jsonObj) {
    if( typeof jsonObj == "object" ) {
        $.each(jsonObj, function(k,v) {
            if(v == nodeID) {
                findNode.node = $(jsonObj).eq(0).toArray()[0];        
            } else {
                findNode.find(nodeID,v); 
            }
        });
    } else {
        //console.log("jsobObj is not an object");
    }
  }
}

which allows me to do the following test:

findNode.find("nodeid_3",json);
alert(findNode.node);

So to sum up – how can I modify a value of a JSON tree that has an unknown depth?

Thanks in advance

  • 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-16T21:46:16+00:00Added an answer on May 16, 2026 at 9:46 pm

    If you want to modify a node, like you said, you can just modify the properties of that node.

    var node = findNode.find("nodeid_3",json);
    node.id = "nodeid_3_modified";
    node.children = [];
    

    Also, why are you using jQuery for this?

    Here’s an alternative without using jQuery should work:

    function findNode(object, nodeId) {
       if (object.id === nodeId) return object;
    
       var result;
       for (var i = 0; i < object.children.length; i++) {
          result = findNode(object.children[i], nodeId);
          if (result !== undefined) return result;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one JSON that is coming in a string format. I need to
I have a JSON result that contains numerous records. I'd like to show the
I have a dijit.Tree that's populated by a ItemFileReadStore using JSON. Because of our
Hi I have a JSON object that is a 2-dimentional array and I need
I have a program written in C that has to input and output JSON
I'd like to create a tree structure from JSON, but with multiple columns that
I have some lengthy JSON text that I am sending back to the server
I have a program that reads financial data in JSON and inserts it into
I have a JSON array with ActiveRecord objects. These objects can be reconstructed using
I have a JSON as follows { columns : [RULE_ID,COUNTRY_CODE], RULE_ID : [1,2,3,7,9,101,102,103,104,105,106,4,5,100,30], COUNTRY_CODE

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.