jsonutil

Convenience functions for working with JSON

jsonutil - Convenience functions for working with JSON

Working with JSON in Node.js is pretty straightforward, but there are a couple of things that could be a bit more convenient. Examples are copying objects, as well as reading them from, and writing them to, files. The jsonutil package addresses just those scenarios with a few handy utility functions.

Just use npm:

npm install jsonutil

Makes a deep copy (a.k.a. clone) of the supplied object. This function reliably copies only what is valid for a JSON object, array, or other element.

Arguments

  • obj, the object of which a copy is to be made.

Makes a shallow (i.e. top level only) copy of the supplied object. This function reliably copies only what is valid for a JSON object, array, or other element.

Arguments

  • obj, the object of which a copy is to be made.

Makes a copy of the supplied object, either shallow or deep, according to the second argument. This function reliably copies only what is valid for a JSON object, array, or other element.

Arguments

  • obj, the object of which a copy is to be made.
  • shallow, a boolean indicating whether the copy should be shallow or deep.

Reads a JSON file and parses it, calling the callback with the resulting object.

Arguments

  • file, the file from which to read and parse JSON.
  • cb(err, obj), a callback function that will be called with either the object read from the file, or an error.

A synchronous version of readFile (see above) that returns the parsed object or throws an error.

Writes a JSON file with the specified data. Serializing the data will fail if the data contains a cyclic structure.

  • file, the file to which the object will be written.
  • obj, the object to be written as JSON.
  • indent, the indentation level, in spaces, of the JSON output. [Default: no indentation]
  • cb(err), a callback function that will be called with either null on success, or an error.

A synchronous version of writeFile (see above) that throws any error.

jsonutil is licensed under the MIT License.