Skip to main content

JSON

The JSON module provides comprehensive JSON (JavaScript Object Notation) capabilities for Soul, including parsing, serialization, and validation. It offers both Soul-style and JavaScript-style aliases for familiar API usage.

Parsing JSON

decode / parse - Parse JSON string to object

Parse a JSON string into Soul objects (maps or lists):

Parsing Complex Structures

Parse nested JSON structures:

Serialization

encode / stringify - Convert object to JSON string

Convert Soul objects (maps or lists) to JSON strings:

Serializing Complex Objects

Serialize nested structures:

Validation

isValid - Check if string is valid JSON

Validate whether a string contains valid JSON:

Data Type Handling

Supported Data Types

The JSON module handles all standard JSON data types:

Map Key Conversion

When encoding maps, different key types are converted to strings:

Error Handling

Parsing Errors

Handle JSON parsing errors gracefully:

Encoding Errors

Handle encoding errors for unsupported types:

Practical Examples

Configuration File Handling

API Response Handling

Data Transformation Pipeline

Working with JSON Arrays

JSON Validation Workflow

Best Practices

  1. Always validate before parsing: Use isValid() to check JSON validity before parsing
  2. Handle errors gracefully: Check for ERROR types when parsing or encoding
  3. Use appropriate aliases: Choose decode/encode or parse/stringify based on your preference
  4. Be aware of type limitations: Only LIST and MAP types can be encoded to JSON
  5. Consider memory usage: Large JSON strings can consume significant memory
The JSON module provides essential tools for working with JSON data in Soul, enabling seamless integration with APIs, configuration files, and data exchange formats commonly used in modern applications.