JSON Database icon

JSON Database

Reads/writes data from/to a JSON file acting as an hierarchical key-value database

Write To Database

AI-generated

Overview

The Write To Database operation of the "JSON Database" node allows you to write data into a JSON file, treating it as a hierarchical key-value database. This is useful for scenarios where you need lightweight, file-based storage for structured data within your n8n workflows—such as logging events, storing configuration, or maintaining state between workflow executions.

Practical examples:

  • Storing user preferences or session data in a JSON file.
  • Logging processed items or errors for later review.
  • Updating specific fields in a shared JSON document used by multiple workflows.

Properties

Name Meaning
Query Path Path on which to act in the JSON tree (e.g., monsters["cookie-monster"].scares). Leave empty to select the root of the JSON structure.
Data Source The source of the data to write at the chosen database path. Options:
- Object Key: Use a value from the current input context.
- JSON String: Provide an arbitrary JSON string to be parsed and written.
Source Object Key (Shown if Data Source = Object Key) The key from the current input context whose value will be written to the database.
Source JSON String (Shown if Data Source = JSON String) A JSON string to be evaluated as an object. If left empty, this means deletion from the database at the specified path.
File Path Path to the JSON file to read/write. Leave empty to use the default global database file.

Output

The output is a single field:

{
  "data": <value_written>
}
  • data: The value that was written to the specified path in the JSON database. This could be any valid JSON type (object, array, string, number, boolean, or null).

If an error occurs and "Continue On Fail" is enabled, the output will instead be:

{
  "error": "<error message>"
}

Dependencies

  • File System Access: The node reads from and writes to files on the server where n8n is running. Ensure the n8n process has appropriate permissions for the target directory and files.
  • No external API keys required.
  • Uses the bundled lodash library for deep property access.

Troubleshooting

Common Issues:

  • File Permission Errors:
    If n8n does not have permission to read/write the specified file or directory, the node will throw an error.
    Resolution: Check and adjust file system permissions.

  • Database Lock Timeout:
    If the database file is locked for too long (e.g., due to another process or a previous crash), you may see:
    "The database is being kept locked for too much time. Try again later."
    Resolution: Wait and retry, or manually remove the lock directory if safe.

  • Invalid JSON Input:
    If "Source JSON String" contains invalid JSON, an error will be thrown.
    Resolution: Validate your JSON before submitting.

  • Single Value at Root:
    Attempting to write a primitive value (string, number, boolean, or null) to the root of the database is not allowed.
    Error: "Single item values cannot be assigned to the JSON root."
    Resolution: Write objects or arrays at the root level.

Links and References

Discussion