-1

I don't know how to append my new object into already exist .json file I run this program only in local

function writeJson(){
    var tempObj =  '{"name" : "kkk", "age":"123", "location" : "123123"}';
    var jsonObj = JSON.parse(tempObj);

    $.getJSON("data.json", function(data) {
        console.log(data);
    });

I will wait your answer

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
곽성훈
  • 19
  • 1
  • 4
  • is it that you want to add stuff to `tempObj` ? – Prashanth Benny Feb 20 '17 at 08:35
  • 1
    What do you mean by "append"? Do you want to assign the value of `data` to a (new?) property of `jsonObj`? Or do you want to merge the value of `data` into `jsonObj` (assuming it is an object)? What do you want the result to be? Why don't your provide an example? – Felix Kling Feb 20 '17 at 08:35
  • This might help: http://stackoverflow.com/questions/12290572/appending-to-json-file-in-javascript – mindOfAi Feb 20 '17 at 08:36
  • i want to insert jsonObj into .json file – 곽성훈 Feb 20 '17 at 08:36
  • What does the original JSON file look like, and what should it look like after? – Barmar Feb 20 '17 at 08:37
  • 2
    *"i want to insert jsonObj into .json file"* Do you mean to update the *file* on the server? You cannot do that with only client side JavaScript. Please read [ask] and [mcve] for guidance on how to properly ask a question. – Felix Kling Feb 20 '17 at 08:37
  • 1
    That's still not clear. Do you mean that you want to add the `jsonObj` to the *data returned from the AJAX call*, or that you want to *amend the file as it's stored on the server*? They are very different tasks. – Rory McCrossan Feb 20 '17 at 08:37
  • If you stick to JavaScript then nodejs is helpful in writing files. – Vilas Kumkar Feb 20 '17 at 08:38
  • [ { "name": "이름", "age": "나이", "location": "지역" }, { "name": "111", "age": "28", "location": "영등포" } – 곽성훈 Feb 20 '17 at 08:39
  • this is my .json file – 곽성훈 Feb 20 '17 at 08:39
  • and i want to add one object by using above code – 곽성훈 Feb 20 '17 at 08:40
  • As I said, you cannot use client side JavaScript to change the file on the server. Your webserver needs to provide an endpoint that accepts data and updates the file. How to do that depends on the webserver and the server side language you are using. – Felix Kling Feb 20 '17 at 08:41
  • i didn't use concept of server and client. all file are in local – 곽성훈 Feb 20 '17 at 08:43
  • Then you are out of luck. Browsers cannot write to the local file system. The only thing you could do is trigger the download of a (generated) file and let the user choose where to store it. See [Using HTML5/Javascript to generate and save a file](http://stackoverflow.com/q/2897619/218196) – Felix Kling Feb 20 '17 at 08:44
  • then is there no way to change the file by using user inputting data – 곽성훈 Feb 20 '17 at 08:47

1 Answers1

0

I think you need to change the format of your JSON object so that something like this will work for you:

var data = JSON.parse('{"John":{"name":"John","age":30,"city":"New York"},"Jeff":{"name":"Jeff","age":32,"city":"New Jersey"}}');
// append
data["Billy"] = JSON.parse('{"name":"Billy","age":64,"city":"Liverpool"}');
HomerPlata
  • 1,687
  • 5
  • 22
  • 39