Skip to content Skip to sidebar Skip to footer

How To Add The New JSON From Existing JSON File

I searched here get few related posts found but not helpful. I created one json file it has some text i want to append some more json in that using javascript(JSON stored in locall

Solution 1:

You need to read your JSON, parse it into a JS object, edit the object, and convert back into JSON before writing:

assuming myfile.json contains: { "Home": [ "a", "b", "c" ] }

$.getJSON('myfile.json', function(data) {
    alert("success");
    obj = JSON.parse(data);
    obj.log = 1;
    writeJSON(JSON.stringify(obj));
}.error(function(data){
    alert(JSON.stringify(data));
});

and writeJSON will be something like:

function writeJSON(jsonString)
    $.ajax({
        type: 'POST',
        url: 'myfile.json',
        data: jsonString,
        success: function(data) { alert('write succesful!'); },
        contentType: "application/json",
        dataType: 'json'
    });
}

assuming that you are using a server which can both read and write via this endpoint.


Post a Comment for "How To Add The New JSON From Existing JSON File"