7

I currently have an object:

var obj = {
        username: "James",
        surname: "Brandon",
        id: "[2]"
}

and I want to append it to "users.json":

[
  {    
    "username": "Andy",
    "surname": "Thompson",
    "id": [0],
  },
  {    
    "username": "Moe",
    "surname": "Brown",
    "id": [1]
  }
]

Do you know how I might be able to do this? Thanks in advance.

James Morgson
  • 131
  • 1
  • 1
  • 8
  • 1
    `users.push(obj)`? This is assuming `users` is the second array you posted. – slider Oct 29 '18 at 22:25
  • 2
    Give us more detail about the output .json - is this a file to produce, is it an output? Are you running in node.js or browser? – Deian Oct 29 '18 at 22:26
  • Well you can add it, but it will not save it to the file on the server... – epascarello Oct 29 '18 at 22:31
  • 1
    You cannot push to the end of a JSON file using push. You need to load the file, then parse it using `var val = JSON.parse(textFromFile)` you can then use push to push it onto the end of that val `val.push({/*new object*/})`, then you will need to stringify the val `var newString = JSON.strigify(val)` and then you finally can save `newString` to a file. – Get Off My Lawn Oct 29 '18 at 22:36
  • You want to do that in a browser, or in node? – baao Oct 29 '18 at 22:44
  • @Deian users.json is just an ourput file that's running in browser. – James Morgson Oct 29 '18 at 22:45
  • Again, if the file is on the server, there is no way to update the file from the clientside. You would need to have code running on the server that would update the file.... – epascarello Oct 29 '18 at 22:49
  • Tell us what you want to do with the updated file (make it available to the client by a download, uploaded to the server...) – David Oct 29 '18 at 22:59

2 Answers2

6

This answer is assuming that you are working under Node.js.

As I understand your problem you need to solve a few different programming questions.

  1. read and write a .json file

    const fs = require("fs");
    let usersjson = fs.readFileSync("users.json","utf-8");
    
  2. transform a json string into a javascript array

    let users = JSON.parse(usersjson);
    
  3. append an object to an array

    users.push(obj);
    
  4. transform back the array into a json string

    usersjson = JSON.stringify(users);
    
  5. save the json file

    fs.writeFileSync("users.json",usersjson,"utf-8");
    
David
  • 6,695
  • 3
  • 29
  • 46
PA.
  • 28,486
  • 9
  • 71
  • 95
  • The only thing was that it complains that require isn't defined and I'm not sure how to include it. – James Morgson Oct 29 '18 at 22:42
  • 1
    This answer is assuming it is node and it appears that it ain't node the OP is after. – epascarello Oct 29 '18 at 22:48
  • 1
    ```users.push(obj)``` does not work, you need to convert users from any to Array first: ```const arr = Array.from(users);```, and then you can use ```arr.push(obj)``` and so on. – dancus25 Apr 19 '22 at 15:16
2

If your code is running in the browser and users.json is an output file, I guess you already have access to its content.

Use the push() method.

Also, note the missing commas in your objects.

var obj = {
        username: "James",
        surname: "Brandon",
        id: "[2]"
};

var users = [
  {    
    "username": "Andy",
    "surname": "Thompson",
    "id": [0]
  },
  {    
    "username": "Moe",
    "surname": "Brown",
    "id": [1]
  }
];

users.push(obj);
console.log( JSON.stringify(users) );

Now that you have the updated array of objects you can upload it to the server (check this question) or offer a download to the user (check this other question).

As you have been already told, there is no way to directly update client-side a file in the server. It is also not possible to save it directly into the client filesystem.

David
  • 6,695
  • 3
  • 29
  • 46