Implementation:CrewAIInc CrewAI RAG JSON Loader
| Knowledge Sources | |
|---|---|
| Domains | RAG, Data_Loading |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Loads and parses JSON files from local paths or URLs, converting structured data into human-readable text format for RAG applications.
Description
JSONLoader extends BaseLoader to handle JSON data from multiple sources. It reads content from local files (with UTF-8 encoding) or fetches from URLs using the shared load_from_url utility with an "application/json" accept header.
The _parse_json() method processes the parsed data based on its structure:
- Dictionaries are formatted as key-value pairs, one per line, with values serialized using json.dumps with no indentation.
- Lists are formatted as individual items, each serialized with json.dumps.
- Primitives are directly stringified with json.dumps.
The returned LoaderResult includes metadata about the format ("json"), the Python type of the root data structure, and the size (number of items for dicts/lists, or 1 for primitives). Parse errors from json.JSONDecodeError are handled gracefully by returning the raw content with error details in the metadata.
Usage
Import JSONLoader when you need to explicitly load JSON data. It is typically instantiated automatically by the DataType.JSON registry when .json files or URLs with JSON content are detected.
Code Reference
Source Location
- Repository: CrewAI
- File: lib/crewai-tools/src/crewai_tools/rag/loaders/json_loader.py
- Lines: 1-56
Signature
class JSONLoader(BaseLoader):
def load(self, source_content: SourceContent, **kwargs) -> LoaderResult: ...
Import
from crewai_tools.rag.loaders.json_loader import JSONLoader
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| source_content | SourceContent | Yes | Wraps a JSON file path, URL, or raw JSON string |
| **kwargs | Any | No | Additional keyword arguments passed to URL loading |
Outputs
| Name | Type | Description |
|---|---|---|
| return | LoaderResult | Contains formatted text content, source reference, and metadata (format, type, size) |
Usage Examples
Basic Usage
from crewai_tools.rag.loaders.json_loader import JSONLoader
from crewai_tools.rag.source_content import SourceContent
loader = JSONLoader()
# Load from a local file
source = SourceContent("/path/to/config.json")
result = loader.load(source)
print(result.content)
# name: "my-project"
# version: "1.0.0"
# dependencies: {"requests": "^2.28.0"}
print(result.metadata)
# {'format': 'json', 'type': 'dict', 'size': 3}
# Load from a URL
source = SourceContent("https://api.example.com/data.json")
result = loader.load(source)