11

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.

8
  • 1
    users.push(obj)? This is assuming users is the second array you posted. Commented Oct 29, 2018 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? Commented Oct 29, 2018 at 22:26
  • Well you can add it, but it will not save it to the file on the server... Commented Oct 29, 2018 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. Commented Oct 29, 2018 at 22:36
  • You want to do that in a browser, or in node? Commented Oct 29, 2018 at 22:44

2 Answers 2

10

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");
    
Sign up to request clarification or add additional context in comments.

3 Comments

The only thing was that it complains that require isn't defined and I'm not sure how to include it.
This answer is assuming it is node and it appears that it ain't node the OP is after.
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.
3

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.

Comments

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.