Skip to main content
This json splitter splits json data while allowing control over chunk sizes. It traverses json data depth first and builds smaller json chunks. It attempts to keep nested json objects whole but will split them if needed to keep chunks between a min_chunk_size and the max_chunk_size. If the value is not a nested json, but rather a very large string the string will not be split. If you need a hard cap on the chunk size consider composing this with a Recursive Text splitter on those chunks. There is an optional pre-processing step to split lists, by first converting them to json (dict) and then splitting them as such.
  1. How the text is split: json value.
  2. How the chunk size is measured: by number of characters.
First we load some json data:

Basic usage

Specify max_chunk_size to constrain chunk sizes:
To obtain json chunks, use the .split_json method:
To obtain LangChain Document objects, use the .create_documents method:
Or use .split_text to obtain string content directly:

How to manage chunk sizes from list content

Note that one of the chunks in this example is larger than the specified max_chunk_size of 300. Reviewing one of these chunks that was bigger we see there is a list object there:
The json splitter by default does not split lists. Specify convert_lists=True to preprocess the json, converting list content to dicts with index:item as key:val pairs:
Let’s look at the size of the chunks. Now they are all under the max
The list has been converted to a dict, but retains all the needed contextual information even if split into many chunks: