2

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 locally).

I have json file like this:

{ "Home": [ "a", "b", "c" ] }

i want to include this text "Log": 1

want to achieve like this,

{ "Home": [ "a", "b", "c" ], "Log": 1 }

Now I have like this in my json file(currently i have but json format is not correct)

{ "Home": [ "a", "b", "c" ] } 
{ "Log": 1 }

$.getJSON('myfile.json', function(data) {
    alert("success");
}.error(function(data){
    alert(JSON.stringify(data));
});

this returns parser error. I know the JSON format is wrong. Please guide me to create a correct JSON format.

4
  • Can you show the code you use to write and append to the json file as this is the problem? Commented Nov 27, 2014 at 14:05
  • @LcLk ` var homeicons = { "Home" : g };` var a = JSON.stringify(homeicons); Commented Nov 27, 2014 at 14:07
  • { "Home": [ "a", "b", "c" ], "Log": 1 }(a) or [{ "Home": [ "a", "b", "c" ] } , { "Log": 1 }](b), you can't just append data to a json string and get a working json string. If you go with (b) you can achieve (a) with var array = /* data from (b) */; var result = {}; $.extend.apply($, [{}] + array);; Commented Nov 27, 2014 at 14:50
  • @Prusse please provide more detail.. Commented Nov 27, 2014 at 16:29

2 Answers 2

4

The error is in both versions of the JSON. You are missing the " at the start of the string c.

http://jsonlint.com/ will help you track down where errors in JSON occur.

As a rule of thumb, you should create JSON using a JSON serializer in a mature library and not by hand or string concatenation.


Now i have like this in my json file

That's completely wrong. The code you had in "Now i want to achieve like this" was right (aside from the error mentioned above).

Sign up to request clarification or add additional context in comments.

7 Comments

in jsonlint the following format is ok..{ "Home": [ "a", "b", "c" ], "Log": 1 }
@Anu — Yes, that's what I said.
@Anu — Like that! You just put that in the file!
i already have some json in file want to insert new json/values in that json..please see my post...
@Anu — That part of your post doesn't make sense. You're talking about JSON that is stored locally but you are accessing it remotely using HTTP.
|
3

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.

5 Comments

the problem is i called my javascript file, in another script file using $.getScript...now the the script is not loaded....
What exactly is the problem? Can't you just update both scripts and try again? What server are you using anyway, I think there may be more problems here than meet the eye.
this myfile.js have the $.getJson function ...when remove the $getJson the js file is loaded...otherwise not loaded...
Can you add all of your code to a jsfiddle, or at least include it in the original question as a snippet? Not your entire project but the myFile.js, the file which loads it and the server which deals with the json request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.