A lightweight TypeScript library for appending JSON objects to array-based JSON files.
npm install coolice
import { appendJSON } from "coolice"
// Basic usage
appendJSON("data.json", { name: "John" })
// With options
appendJSON(
"data.json",
{ name: "John" },
{
force: true, // Force create new file if JSON is invalid
space: 2, // Indentation spaces
replacer: null // Custom JSON replacer
}
)
// output: [{ "name": "John" }] --> data.json
Appends a JSON object to an array in a JSON file. If the file doesn't exist, creates a new file with the data.
-
path (string) - The file path where the JSON data should be appended
-
data (object) - The object to append to the JSON array
-
options (optional) - Configuration options:
-
force (boolean) - Whether to force create new file if JSON is invalid
-
space (string|number) - Indentation spaces or characters to use
-
replacer (function|array) - Custom replacer function for JSON.stringify
-
MIT