Node.js append to json file (2024)
In this tutorial, we will learn how append or add the json data to json file in Node.js application using javascript.
Node.js Tutorial :
- Install Node.js on Windows
- Node.js Chalk Color Example
- Node.js - Read command line arguments
- Node.js Read Write File Example
- Node.js append to json file
Q: What is fs
module in Nodejs?
Ans:
Node.js provides an inbuilt component called FS (File System). The Node. js file system module enables us to interact with our computer's file system.
Import NPM modules
- Create new folder for the project and open it in Visual Studio Editor.
- You can install Visual Studio Editor.
- Create new file
student-app.js
under the project or while executing below npm command will point to default file calledindex.js
- The
npm init
command will install or create the package.json file if you run it as follows.D:\nodejs\file-example>npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help init` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. package name: (file-example) version: (1.0.0) description: Nodejs file example entry point: (student-app.js) test command: git repository: keywords: author: techgeeknext license: (ISC) About to write to D:\nodejs\file-example\package.json: { "name": "file-example", "version": "1.0.0", "description": "Nodejs file example", "main": "student-app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "techgeeknext", "license": "ISC" } Is this OK? (yes) yes
package.json
{ "name": "file-example", "version": "1.0.0", "description": "Nodejs File Sysytem example", "main": "student-app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "techgeeknext", "license": "ISC" }
Node.js Append to Json File
To append json data/elements to a json file, read the file, parse the JSON, push the new result to the array, convert into a string, and save it.
const fs = require('fs')
//check if file exist
if (!fs.existsSync('student.json')) {
//create new file if not exist
fs.closeSync(fs.openSync('student.json', 'w'));
}
// read file
const file = fs.readFileSync('student.json')
const data = {
studentName: 'Joe',
address: 'abc'
}
//check if file is empty
if (file.length == 0) {
//add data to json file
fs.writeFileSync("student.json", JSON.stringify([data]))
} else {
//append data to jso file
const json = JSON.parse(file.toString())
//add json element to json object
json.push(data);
fs.writeFileSync("student.json", JSON.stringify(data))
}
Run the Node.js to append the data
Node.js Write/Append to json File Javascript:
Run the below command in Terminal to add students to json file.node student-app.js
Json File
We can see data is added/appended tostudent.json
file.[{"studentName":"Joe","address":"abc"}]