Skip to main content

Json Decode

Parses a JSON string into a JSON object.

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 String - The JSON string to be parsed into an object. Cannot be empty.

Options

This node does not have any options.

Outputs

  • Json - The parsed JSON object.

How It Works

The Json Decode node converts a JSON string to a JavaScript object. When executed, the node:

  1. Receives a JSON string through the Json String input
  2. Uses JSON unmarshaling to parse the string into an object
  3. Outputs the parsed object through the Json output

This is essential for processing JSON data from external sources like APIs, files, or databases.

Example Usage

Example 1: Parsing API Response

Parse a JSON response from an HTTP request:

// Input string:
'{"status":"success","data":{"id":123,"name":"Product"}}'

// Output object:
{
status: "success",
data: {
id: 123,
name: "Product"
}
}

Example 2: Reading JSON from File

Parse JSON content read from a file:

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

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

Example 3: Parsing JSON Array

Convert a JSON array string to an array object:

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

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

Example 4: Complex Nested Structures

Handle deeply nested JSON structures:

// Input string:
'{"users":[{"id":1,"name":"Alice","roles":["admin","user"]},{"id":2,"name":"Bob","roles":["user"]}]}'

// Output object:
{
users: [
{
id: 1,
name: "Alice",
roles: ["admin", "user"]
},
{
id: 2,
name: "Bob",
roles: ["user"]
}
]
}

Requirements

  • Valid JSON string as input
  • Input string must not be empty
  • String must conform to JSON specification

Error Handling

The node will return specific errors in the following cases:

  • ErrInvalidArg - When the input string is empty
  • ErrInvalidArg - When the string is not valid JSON format

Usage Tips

  • API Integration: Parse JSON responses from REST APIs
  • File Processing: Decode JSON content from configuration or data files
  • Data Validation: Validate JSON format before further processing
  • Pair with Encode: Use this to reverse Json Encode operations
  • Error Handling: Enable "Continue On Error" when processing untrusted JSON sources
  • Object Access: Use JSON package nodes (Get, Set, etc.) to manipulate the parsed object

Common Use Cases

  1. REST API Integration: Parse JSON responses from HTTP requests
  2. Configuration Loading: Read and parse JSON configuration files
  3. Data Processing: Parse JSON data from databases or message queues
  4. Web Scraping: Extract and parse JSON data embedded in web pages
  5. File Import: Import JSON data files for processing
  6. Webhook Processing: Parse JSON payloads from incoming webhooks

Best Practices

  • Validation: Verify the string is valid JSON before parsing
  • Error Handling: Implement proper error handling for malformed JSON
  • Schema Validation: Consider validating the parsed object structure
  • Security: Be cautious when parsing JSON from untrusted sources
  • Encoding: Ensure the input string uses proper UTF-8 encoding

Common Errors and Solutions

Invalid JSON Format

Problem: Error message "Invalid JSON string"

Solutions:

  • Verify the string is properly formatted JSON
  • Check for missing quotes, commas, or brackets
  • Use a JSON validator to identify syntax errors
  • Ensure special characters are properly escaped
  • Remove trailing commas (not allowed in JSON)

Empty Input

Problem: Error message "JSON string cannot be empty"

Solutions:

  • Verify the input variable contains data
  • Check if the previous node properly set the output
  • Add conditional logic to handle empty values

Syntax Errors

Problem: Parsing fails due to syntax errors

Solutions:

  • Check for unescaped quotes in string values
  • Verify all brackets and braces are properly closed
  • Remove comments (JSON doesn't support comments)
  • Ensure property names are in double quotes
  • Verify boolean values are lowercase (true/false, not True/False)

Character Encoding Issues

Problem: Parsing fails with special characters

Solutions:

  • Ensure the input is UTF-8 encoded
  • Check for byte order marks (BOM) that might interfere
  • Verify escaped unicode characters are properly formatted (\uXXXX)

Examples of Valid vs Invalid JSON

Valid JSON

// Object
'{"name":"John","age":30}'

// Array
'[1,2,3]'

// Nested
'{"data":{"items":[1,2,3]}}'

// With null
'{"value":null}'

// With boolean
'{"active":true}'

Invalid JSON

// Single quotes (must use double quotes)
"{'name':'John'}"

// Trailing comma
'{"name":"John","age":30,}'

// Unquoted property names
'{name:"John"}'

// Comments (not allowed)
'{"name":"John" /* comment */}'

// Undefined value
'{"value":undefined}'

Notes

  • The output object can contain nested objects and arrays
  • All JSON data types are supported: string, number, boolean, null, object, array
  • The parser is strict and follows JSON specification (RFC 7159)
  • Property names are case-sensitive
  • The parsed object can be used with JSON package nodes for further manipulation
  • Very large JSON strings may impact performance
  • The node supports any valid JSON structure regardless of depth