Skip to main content

Json Encode

Converts a JSON object to its string representation.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.

Inputs

  • Json Object - The JSON object to be converted to a string. Cannot be null.

Options

This node does not have any options.

Outputs

  • Json - The string representation of the JSON object.

How It Works

The Json Encode node converts a JSON object to its string format. When executed, the node:

  1. Receives a JSON object through the Json Object input
  2. Uses JSON marshaling to serialize the object to a string
  3. Outputs the JSON string through the Json output

This is essential for storing JSON data as text or transmitting it over text-based protocols.

Example Usage

Example 1: Converting Object for API Request

Convert a JavaScript object to JSON string for HTTP request body:

// Input object:
{
"name": "John Doe",
"email": "john@example.com",
"age": 30
}

// Output string:
'{"name":"John Doe","email":"john@example.com","age":30}'

Example 2: Saving JSON to File

Prepare JSON data for file storage:

// Input object:
{
"config": {
"timeout": 5000,
"retries": 3
},
"enabled": true
}

// Output string:
'{"config":{"timeout":5000,"retries":3},"enabled":true}'

Example 3: Converting Array to String

Convert an array to JSON string:

// Input object:
["apple", "banana", "orange"]

// Output string:
'["apple","banana","orange"]'

Example 4: Nested Objects

Handle complex nested structures:

// Input object:
{
"user": {
"name": "Alice",
"address": {
"city": "New York",
"zip": "10001"
}
},
"items": [
{"id": 1, "name": "Product A"},
{"id": 2, "name": "Product B"}
]
}

// Output string (formatted for readability):
'{"user":{"name":"Alice","address":{"city":"New York","zip":"10001"}},"items":[{"id":1,"name":"Product A"},{"id":2,"name":"Product B"}]}'

Requirements

  • Valid JSON object as input
  • Input object cannot be null

Error Handling

The node will return specific errors in the following cases:

  • ErrInvalidArg - When the input object is null or empty
  • ErrInvalidArg - When JSON marshaling fails (e.g., circular references)

Usage Tips

  • API Integration: Convert objects to JSON strings for HTTP request bodies
  • File Storage: Encode objects as strings before writing to files
  • Data Transmission: Prepare data for sending over text-based protocols
  • Logging: Convert objects to strings for logging purposes
  • Message Queues: Serialize objects for message queue systems
  • Reversible: Use Json Decode node to convert strings back to objects

Common Use Cases

  1. REST API Requests: Convert request body objects to JSON strings
  2. Configuration Files: Save configuration objects as JSON text files
  3. Database Storage: Store JSON data in text columns
  4. Message Passing: Serialize objects for inter-process communication
  5. Data Export: Convert structured data to JSON format for export
  6. Webhook Payloads: Prepare JSON payloads for webhook calls

Best Practices

  • Validation: Ensure the object is valid before encoding
  • Error Handling: Enable "Continue On Error" when processing dynamic data structures
  • Data Types: Remember that functions and undefined values won't be serialized
  • Circular References: Avoid objects with circular references as they cannot be serialized
  • Large Data: Be mindful of memory usage when encoding very large objects

Common Errors and Solutions

Null Object Error

Problem: Error message "JSON object cannot be empty"

Solutions:

  • Verify the input object exists and is not null
  • Check if the previous node properly set the object
  • Add conditional logic to handle null cases

Marshaling Failure

Problem: Error message "Failed to encode JSON"

Solutions:

  • Check for circular references in the object
  • Verify all object properties have serializable values
  • Remove functions or undefined properties from the object
  • Ensure numeric values are not NaN or Infinity

Unexpected Output Format

Problem: JSON string doesn't match expected format

Solutions:

  • Remember that the output is a compact JSON string (no whitespace)
  • Use a JSON formatter if human-readable output is needed
  • Verify the input object structure is correct

Notes

  • The output is a compact JSON string without formatting or whitespace
  • All string values in the output will be properly escaped with quotes
  • Numbers, booleans, and null are serialized as JSON primitives
  • Arrays and nested objects are fully supported
  • The encoding follows standard JSON specification (RFC 7159)
  • Functions and undefined values are omitted from the output
  • Date objects are converted to ISO 8601 strings