Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Appending object to JSON File

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.

like image 769
James Morgson Avatar asked Oct 29 '18 22:10

James Morgson


People also ask

How to get a JavaScript object from a JSON file?

This guide will demonstrate how to get a JavaScript object from a JSON file or access it using a fetch () HTTP request. Any JSON data can be consumed from different sources like a local JSON file by fetching the data using an API call.

How to assign a JavaScript object to a variable in JSON?

We can use a JavaScript object’s JSON format and assign it to variables using the JSON.stringify () method. JSON.stringify () converts the javascript object and returns the JSON value for the object as string data. The function takes the JavaScript object as a parameter, accepts a replacer function, and space count as optional parameters.

What is JSON and how to use it?

JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article. First of all, you need to ensure that the JSON file that you are expecting is not going to consume a large memory.

How do you write JSON arrays?

JSON arrays are written inside square brackets. In the example above, the object "employees" is an array. It contains three objects. Each object is a record of a person (with a first name and a last name).


2 Answers

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");
    
like image 90
PA. Avatar answered Sep 22 '22 21:09

PA.


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.

like image 41
David Avatar answered Sep 22 '22 21:09

David