Load and Process JSON Data with LlamaIndex
Load JSON files into LlamaIndex with control over traversal depth and fragment collapsing.
Why it matters
Integrate JSON data into LlamaIndex for advanced querying and analysis. This asset enables efficient data loading and preparation for AI-driven insights.
Outcomes
What it gets done
Load JSON and JSONL files into LlamaIndex documents.
Control JSON traversal depth and fragment collapse length.
Clean and format JSON structures for better processing.
Utilize as a tool within LangChain agents.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-reader-readers-json | bash Overview
LlamaIndex Readers Integration: Json
A LlamaIndex reader that loads JSON and JSONL files with configurable tree traversal depth, fragment collapsing, and formatting cleanup. Use when structured JSON or JSONL data needs controlled flattening into indexable documents.
What it does
The JSON Reader loads JSON documents into LlamaIndex while giving control over how the JSON tree is traversed and represented as text. It can extract relationships between nodes in the structure, control how deep into the tree it traverses, collapse long JSON fragments so they don't dominate the output, and clean up the JSON structure before it becomes document text.
JSONReader is configured with several options: levels_back sets how many levels up the tree to traverse (0 traverses all levels), collapse_length caps how many characters a JSON fragment can reach before being collapsed in the output, ensure_ascii forces ASCII-encoded output, is_jsonl tells the reader the input is JSON Lines format rather than a single JSON document, and clean_json (default True) strips lines that are pure formatting with no content. load_data then takes an input file path and optional extra metadata.
When to use - and when NOT to
Use it when you need to turn structured JSON data - configuration files, API responses, or JSONL datasets - into text documents for indexing, and you need control over how deeply nested structures get flattened or collapsed rather than a naive full dump. Use is_jsonl specifically when the input file is JSON Lines rather than a single JSON object or array. Do not use it for non-JSON file formats - it is purpose-built for JSON and JSONL input.
Capabilities
load_data reads a JSON or JSONL file and returns LlamaIndex Document objects, with traversal depth, fragment collapsing, ASCII encoding, JSONL mode, and line-cleaning all configurable on the reader.
How to install
pip install llama-index-readers-json
Who it's for
Developers who need to index structured JSON or JSONL data into LlamaIndex and want control over traversal depth and output cleanliness rather than a raw dump of the file.
Source README
LlamaIndex Readers Integration: Json
Overview
JSON Reader reads JSON documents with options to help extract relationships between nodes. It provides functionalities to control the depth of JSON traversal, collapse long JSON fragments, and clean JSON structures.
Installation
You can install JSON Reader via pip:
pip install llama-index-readers-json
Usage
from llama_index.readers.json import JSONReader
### Initialize JSONReader
reader = JSONReader(
# The number of levels to go back in the JSON tree. Set to 0 to traverse all levels. Default is None.
levels_back="<Levels Back>",
# The maximum number of characters a JSON fragment would be collapsed in the output. Default is None.
collapse_length="<Collapse Length>",
# If True, ensures that the output is ASCII-encoded. Default is False.
ensure_ascii="<Ensure ASCII>",
# If True, indicates that the file is in JSONL (JSON Lines) format. Default is False.
is_jsonl="<Is JSONL>",
# If True, removes lines containing only formatting from the output. Default is True.
clean_json="<Clean JSON>",
)
### Load data from JSON file
documents = reader.load_data(input_file="<Input File>", extra_info={})
This loader is designed to be used as a way to load data into
LlamaIndex and/or subsequently
used as a Tool in a LangChain Agent.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.