jsonfile vs write-json-file vs lowdb
JSON File Manipulation Comparison
1 Year
jsonfilewrite-json-filelowdbSimilar Packages:
What's JSON File Manipulation?

JSON file manipulation libraries in Node.js provide tools for reading, writing, and updating JSON data stored in files. These libraries simplify tasks such as loading configuration settings, storing application state, or managing structured data in a file system. They handle the complexities of parsing JSON, serializing JavaScript objects, and ensuring data integrity during read and write operations. By using these libraries, developers can efficiently manage JSON data in their applications, whether for simple tasks or more complex data management needs.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
jsonfile63,476,1481,206-54 years agoMIT
write-json-file2,182,4722227.41 kB17 months agoMIT
lowdb945,16621,81322.9 kB13a year agoMIT
Feature Comparison: jsonfile vs write-json-file vs lowdb

File Reading and Writing

  • jsonfile:

    jsonfile provides simple methods for reading and writing JSON files. It handles parsing and stringifying JSON automatically, making it easy to work with JSON data without worrying about the underlying file operations.

  • write-json-file:

    write-json-file focuses on writing JSON files efficiently. It provides options for ensuring the output is well-formatted, and it can handle streams, making it suitable for writing large JSON data.

  • lowdb:

    lowdb allows for reading and writing JSON data with a more structured approach. It supports nested data and provides a simple API for querying and updating data, making it more versatile for managing complex JSON structures.

Data Structure

  • jsonfile:

    jsonfile works with plain JavaScript objects and arrays. It does not impose any structure on the data, allowing for flexibility in how JSON is organized.

  • write-json-file:

    write-json-file handles standard JSON data structures (objects and arrays) but does not provide any built-in support for managing or querying nested data.

  • lowdb:

    lowdb uses a JSON file as a database, allowing for more structured data management. It supports nested objects and arrays, enabling more complex data relationships.

Ease of Use

  • jsonfile:

    jsonfile is very easy to use, with a minimal API that requires little setup. It is ideal for quick tasks and simple projects where you need to read or write JSON files without any hassle.

  • write-json-file:

    write-json-file is straightforward for writing JSON files, especially when you need specific features like pretty-printing or ensuring directories exist. However, it is more specialized than the others.

  • lowdb:

    lowdb has a slightly steeper learning curve due to its database-like features, but it is still user-friendly. It is well-documented and provides a clear API for managing data.

Streaming Support

  • jsonfile:

    jsonfile does not support streaming for reading or writing files. It reads the entire file into memory, which can be a limitation for large files.

  • write-json-file:

    write-json-file supports streaming when writing files, allowing for more efficient handling of large data sets. This feature makes it a better choice for applications that need to write large amounts of data without consuming too much memory.

  • lowdb:

    lowdb also does not support streaming. It reads and writes the entire JSON file, which can be inefficient for large datasets but is manageable for small to medium-sized files.

Code Examples

  • jsonfile:

    Reading and Writing JSON with jsonfile

    const jsonfile = require('jsonfile');
    const file = 'data.json';
    
    // Write JSON to file
    const obj = { name: 'Alice', age: 25 };
    jsonfile.writeFile(file, obj)
      .then(() => console.log('Data written to file'))
      .catch(err => console.error(err));
    
    // Read JSON from file
    jsonfile.readFile(file)
      .then(data => console.log('Data read from file:', data))
      .catch(err => console.error(err));
    
  • write-json-file:

    Writing JSON with write-json-file

    const writeJsonFile = require('write-json-file');
    const file = 'data.json';
    const data = { name: 'Alice', age: 25 };
    
    // Write JSON to file
    writeJsonFile(file, data, { indent: 2 })
      .then(() => console.log('Data written to file'))
      .catch(err => console.error(err));
    
  • lowdb:

    Using lowdb for Simple Data Storage

    const { Low, JSONFile } = require('lowdb');
    const file = 'db.json';
    const adapter = new JSONFile(file);
    const db = new Low(adapter);
    
    // Read data
    await db.read();
    
    // Set default data if empty
    if (!db.data) db.data = { users: [] };
    
    // Add a user
    db.data.users.push({ name: 'Alice', age: 25 });
    await db.write();
    
    console.log('User added:', db.data);
    
How to Choose: jsonfile vs write-json-file vs lowdb
  • jsonfile:

    Choose jsonfile if you need a simple and straightforward solution for reading and writing JSON files. It is lightweight and easy to use, making it ideal for quick tasks and small projects.

  • write-json-file:

    Choose write-json-file if you need a focused solution for writing JSON files with options for pretty-printing, ensuring directories exist, and handling file streams. It is particularly useful when you want more control over the writing process.

  • lowdb:

    Choose lowdb if you require a simple, file-based database with support for reading and writing JSON data. It provides a more structured way to manage data with a simple API, making it suitable for small to medium-sized applications.

README for jsonfile

Node.js - jsonfile

Easily read/write JSON files in Node.js. Note: this module cannot be used in the browser.

npm Package build status windows Build status

Standard JavaScript

Why?

Writing JSON.stringify() and then fs.writeFile() and JSON.parse() with fs.readFile() enclosed in try/catch blocks became annoying.

Installation

npm install --save jsonfile

API


readFile(filename, [options], callback)

options (object, default undefined): Pass in any fs.readFile options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If JSON.parse throws an error, pass this error to the callback. If false, returns null for the object.
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file, function (err, obj) {
  if (err) console.error(err)
  console.dir(obj)
})

You can also use this method with promises. The readFile method will return a promise if you do not pass a callback function.

const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file)
  .then(obj => console.dir(obj))
  .catch(error => console.error(error))

readFileSync(filename, [options])

options (object, default undefined): Pass in any fs.readFileSync options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If an error is encountered reading or parsing the file, throw the error. If false, returns null for the object.
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'

console.dir(jsonfile.readFileSync(file))

writeFile(filename, obj, [options], callback)

options: Pass in any fs.writeFile options or set replacer for a JSON replacer. Can also pass in spaces, or override EOL string or set finalEOL flag as false to not save the file with EOL at the end.

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, function (err) {
  if (err) console.error(err)
})

Or use with promises as follows:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj)
  .then(res => {
    console.log('Write complete')
  })
  .catch(error => console.error(error))

formatting with spaces:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2 }, function (err) {
  if (err) console.error(err)
})

overriding EOL:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
  if (err) console.error(err)
})

disabling the EOL at the end of file:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2, finalEOL: false }, function (err) {
  if (err) console.log(err)
})

appending to an existing JSON file:

You can use fs.writeFile option { flag: 'a' } to achieve this.

const jsonfile = require('jsonfile')

const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {
  if (err) console.error(err)
})

writeFileSync(filename, obj, [options])

options: Pass in any fs.writeFileSync options or set replacer for a JSON replacer. Can also pass in spaces, or override EOL string or set finalEOL flag as false to not save the file with EOL at the end.

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj)

formatting with spaces:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2 })

overriding EOL:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })

disabling the EOL at the end of file:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2, finalEOL: false })

appending to an existing JSON file:

You can use fs.writeFileSync option { flag: 'a' } to achieve this.

const jsonfile = require('jsonfile')

const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { flag: 'a' })

License

(MIT License)

Copyright 2012-2016, JP Richardson jprichardson@gmail.com