Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export/save updated d3.js v4 tree json data string

I'm using the following code:

https://bl.ocks.org/adamfeuer/042bfa0dde0059e2b288

And am loading a very simple json string to create the tree:

{
    "name": "flare",
    "children": [{
        "name": "analytics"
    }, {
        "name": "animate"
    }]
}

So what I'm trying to figure out is that after I add a new child node to the "flare" node (for example), how can I create an updated json string in order to save the newly added node?

An example of the updated json after adding a new node would be like so:

{
    "name": "flare",
    "children": [{
        "name": "analytics"
    }, {
        "name": "animate"
    }, {
        "name": "NEW NODE"
    }]
}

Is there some built in function for this that I am not finding? Or would a custom function have to be built? And if I need a custom function could somebody please point me in the right direction to do so? Thank you very much!

like image 248
Rick Avatar asked Oct 20 '25 17:10

Rick


1 Answers

I propose this solution that is not perfect and which deserves improvements but that works, it will help you get started.

All the code below is added at the end of the update function in the dndTree.js file.

console.log(root); //root contains everything you need
    const getCircularReplacer = (deletePorperties) => { //func that allows a circular json to be stringified
      const seen = new WeakSet();
      return (key, value) => {
        if (typeof value === "object" && value !== null) {
          if(deletePorperties){
            delete value.id; //delete all properties you don't want in your json (not very convenient but a good temporary solution)
            delete value.x0;
            delete value.y0;
            delete value.y;
            delete value.x;
            delete value.depth;
            delete value.size; 
          }
          if (seen.has(value)) {
            return;
          }
          seen.add(value);
        }
        return value;
      };
    };

    var myRoot = JSON.stringify(root, getCircularReplacer(false)); //Stringify a first time to clone the root object (it's allow you to delete properties you don't want to save)
    var myvar= JSON.parse(myRoot);
    myvar= JSON.stringify(myvar, getCircularReplacer(true)); //Stringify a second time to delete the propeties you don't need

    console.log(myvar); //You have your json in myvar

Now that you have your json, you can either :

  • Download your new tree.json file :

     function download(content, fileName, contentType) {
       var a = document.createElement("a");
       var file = new Blob([content], {
         type: contentType
       });
       a.href = URL.createObjectURL(file);
       a.download = fileName;
       a.click();
     }
     download(myvar, 'tree.json', 'text/plain'); 
    
  • Or you could directly write in your file.

An example with node.js :

    var fs = require('fs');
    fs.writeFile("tree.json", myvar, function(err) {
      if (err) {
        console.log(err);
      }
    });

Check this for more informations to save a file: How do I save JSON to local text file

like image 64
abvlle Avatar answered Oct 23 '25 07:10

abvlle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!