ExamplesRAGChunk JSON

Chunk JSON

When working with JSON data, you need to split it into smaller pieces while preserving the object structure. The chunk method breaks down JSON content intelligently, maintaining the relationships between keys and values. This example shows how to chunk JSON documents for search or retrieval purposes.

import { MDocument } from "@mastra/rag";
 
const testJson = {
  name: "John Doe",
  age: 30,
  email: "john.doe@example.com",
};
 
const doc = MDocument.fromJSON(JSON.stringify(testJson));
 
const chunks = await doc.chunk({
  maxSize: 100,
});
 
console.log(chunks);





View Example on GitHub

MIT 2025 © Nextra.