1

I wrote an html file with javascript code, which reads a json file in flat structure and coverts into tree structure (considering parents of nodes).

The output is stored in

var tree = [];

I fill the tree array by pushing root node ("parent":"null") to tree and pushing other nodes to their respective parent nodes.

Finally I put the following alert statement:

alert(JSON.stringify(tree, null, '  '));

I run the HTML file from a WAMP server. The alert displays the message with the content as expected. But, instead of alert, I want to write the data into a file. How do I do this?

I wrote the following code in the html file. abc.js defines a variable named data which stores json data in flat form. I need to replace alert by writing to file.

<html>
    <head>
        <script src="http://d3js.org/d3.v3.min.js"></script>
        <script src="abc.js"></script>
        <script type="text/javascript">
            // create a name: node map
            var dataMap = data.reduce(function(map, node) {
                map[node.id] = node;
                return map;
            }, {});

            // create the tree array
            var tree = [];

            data.forEach(function(node) {
                // add to parent
                var parent = dataMap[node.parent];

                if (parent) {
                    // create child array if it doesn't exist
                    (parent.children || (parent.children = []))
                        // add node to child array
                        .push(node);
                } else {
                    // parent is null or missing
                    tree.push(node);
                }
            });

            alert(JSON.stringify(tree, null, '  '));
        </script>
    </head>

    <body></body>
</html>
Avinash
  • 385
  • 3
  • 11
  • 26
  • You can't write files from Javascript. – Lars Kotthoff Nov 13 '13 at 14:49
  • Where do you want to store the file? If it's on the user's side, not sure if there is a universal way to do this in Javascript. I know you could write the text to a text stream using PHP http://stackoverflow.com/questions/4371748/create-a-text-file-for-download-on-the-fly – Jimmy Smith Nov 13 '13 at 14:50
  • You could always just return the json content in the page, and nothing else, and then have the user save the page. – NotSimon Nov 13 '13 at 14:53
  • possible duplicate of [How to read and write into file using JavaScript](http://stackoverflow.com/questions/585234/how-to-read-and-write-into-file-using-javascript) – Quentin Nov 13 '13 at 14:55
  • I am running the file from my computer from wamp server and I would like to save in my computer itself. – Avinash Nov 13 '13 at 14:57
  • @Simon, how do I return the json content into page? The variable containing the json is an array named tree. So please let me know how I display it in page. I tried the following: d3.select('body').append('pre') .text(JSON.stringify(tree, null, ' ')); But it does not display anything. Do I need to put something in body tag? – Avinash Nov 13 '13 at 15:02
  • @Simon, thanks. It works now. I am displaying in a div tag and I have put the script after the div tag. So it works now. Thanks. – Avinash Nov 13 '13 at 15:24
  • @Avinash, I'm glad it worked, though you should probably consider why you're doing it this way, and not some other way. It's terribly hack-y. – NotSimon Nov 13 '13 at 19:12
  • No no, this will be used only on the development machine. So no problem. – Avinash Nov 14 '13 at 06:03

0 Answers0