Skip to main content
The user points at JSON data; the agent validates structure and reports issues in plain language.
Prerequisites
  • Python 3.10 or higher
  • PraisonAI Agents package installed
  • PraisonAI Tools package installed
  • Basic understanding of JSON format

JSON Tools

Use JSON Tools to process and manipulate JSON files with AI agents.
1

Install Dependencies

First, install the required packages:
2

Import Components

Import the necessary components:
3

Create Agent

Create a JSON processing agent:
4

Define Task

Define the JSON processing task:
5

Run Agent

Initialize and run the agent:

Understanding JSON Tools

What are JSON Tools?

JSON Tools provide JSON processing capabilities for AI agents:
  • File reading and writing
  • Data validation
  • Schema validation
  • Data transformation
  • Structure analysis

Key Components

JSON Agent

Create specialized JSON agents:

JSON Task

Define JSON tasks:

Process Types

Sequential or parallel processing:

JSON Options

Customize JSON operations:

Available Functions

Function Details

read_json(filepath: str, encoding: str = ‘utf-8’, validate_schema: Optional[Dict[str, Any]] = None)

Reads JSON files with schema validation:
  • Optional schema validation
  • Custom encoding support
  • Error handling

write_json(data: Union[Dict[str, Any], List[Any]], filepath: str, encoding: str = ‘utf-8’, indent: int = 2, sort_keys: bool = False, ensure_ascii: bool = False)

Writes data to JSON files:
  • Pretty printing with indentation
  • Optional key sorting
  • Unicode support
  • Directory creation

merge_json(files: List[str], output_file: str, merge_arrays: bool = True, overwrite_duplicates: bool = True)

Merges multiple JSON files:
  • Deep merging of objects
  • Array handling options
  • Duplicate key handling
  • Nested structure support

validate_json(data: Union[Dict[str, Any], str], schema: Dict[str, Any])

Validates JSON against a schema:
  • JSON Schema support
  • Detailed error messages
  • File or data validation

analyze_json(data: Union[Dict[str, Any], str], max_depth: int = 10)

Analyzes JSON structure:
  • Type information
  • Size metrics
  • Nested structure analysis
  • Sample data

transform_json(data: Union[Dict[str, Any], str], transformations: List[Dict[str, Any]])

Transforms JSON data:
  • Multiple operations
  • Path-based modifications
  • Nested transformations
  • Operation types: set, delete, rename, move

Example Agent Configuration

Dependencies

The JSON tools require the following Python packages:
  • jsonschema: For JSON schema validation
These will be automatically installed when needed.

Error Handling

All functions include comprehensive error handling:
  • File I/O errors
  • JSON parsing errors
  • Schema validation errors
  • Transformation errors
Errors are handled consistently:
  • File operations return bool for success/failure
  • Data operations return error details in result
  • All errors are logged for debugging

Common Use Cases

  1. Configuration Management:
  1. Data Analysis:
  1. Data Transformation:

Examples

Basic JSON Processing Agent

Advanced JSON Operations with Multiple Agents

Best Practices

Configure agents with clear JSON focus:
Define specific JSON operations:

Common Patterns

JSON Processing Pipeline

Custom Tools

Build your own agent tools

Tools Overview

Browse PraisonAI tool documentation