Diego Estrada
Diego Estrada

Reputation: 87

JS - Inserting, Deleting and Adding data in JSON file

I am working on this JavaScript project where all the data is stored in a local JSON file, I have been able to read the information on this file, but I have seem to be lost on how to add and modify information on this JSON file, can someone please point me in to the right direction.

Thanks.

Upvotes: 2

Views: 2195

Answers (3)

Brett Caswell
Brett Caswell

Reputation: 730

Your question is essentially doing File/Directory I/O (read & write) on the contents of a file composed of json (a string, data structure format). In particular, you want to write the contents back to file using Javascript; well, currently,..

There isn't a web standard, or endorsed specification for writing files using javascript. Modern browsers continue to implement their own APIs.

These APIs use aspects of the File API (in particular, the interface for blob). The state of File API is still 'Work in Progress'; the status of the document: 'Last Call Working Draft'. this reiterates the point that there is, currently, no standardized, endorsed javascript implementation for file/directory I/O

Since these are not web standardized, they are not recommended for production; and are not endorsed, recommended by W3C.


Noteworthy: The FileWriter interface inherits from the FileSaver interface, but was only implemented in Chrome's webkit (which Opera inherits now). (Note: it was stated that there is no constructor for the class implementing FileWriter).

Other and past solutions were to use Flash components as a workaround to implement 'Save As' functionality. In Turn, there are polyfills and shims for implementing these experimental or unstandardized specs to unsupported devices/user-agents.


It is still indeterminable to me whether your project here is for internal purposes. If you're building an intranet application or attempting to do a utility script that isn't intended to be standardized and/or publicly accessible in production, then I recommend you review and use these APIs.

Upvotes: 0

dReAmEr
dReAmEr

Reputation: 7196

With the Jquery ,you can try something like below,to fetch the data and then later on you can manipulate it as Java Script object.

<html>
    <head>
        <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
        <script type="text/javascript" charset="utf-8">
            function loadJson() {
                $(document).ready(function()
                    {
                        $.getJSON('file.json', function(json) {
                            console.log(json);
                        });
                    });
                }
        </script>
    </head>
    <body onload="loadJson();">

    </body>
</html>

Upvotes: 1

Maxim Mazurok
Maxim Mazurok

Reputation: 4138

You can use parseJSON jQuery function to convert string to JSON object and then modify it. Then you can use JSON.stringify JavaScript function to convert edited JSON object to string.

Upvotes: 0

Related Questions