@juspay/neurolink
Version:
Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applicatio
103 lines (102 loc) • 3.19 kB
TypeScript
/**
* JSON Extraction Utilities
*
* Utilities for extracting JSON from mixed text content.
* Particularly useful for parsing AI responses that contain JSON within prose.
*/
/**
* Extract JSON string from text that may contain surrounding content.
*
* Searches for valid JSON in the following order:
* 1. Direct parse of the entire text
* 2. JSON within markdown code blocks (```json ... ``` or ``` ... ```)
* 3. JSON object pattern ({ ... })
* 4. JSON array pattern ([ ... ])
*
* @param text - Text that may contain JSON
* @returns Extracted JSON string or null if none found
*
* @example
* ```typescript
* const response = "Here's the data: {\"name\": \"test\"} Let me know if you need more.";
* const json = extractJsonStringFromText(response);
* // Returns: '{"name": "test"}'
* ```
*/
export declare function extractJsonStringFromText(text: string): string | null;
/**
* Extract and parse JSON from mixed text content.
*
* Useful for parsing AI responses that contain JSON within prose.
* Combines extraction and parsing in one step.
*
* @param text - Text that may contain JSON
* @returns Parsed JSON value or null if not found/invalid
*
* @example
* ```typescript
* const response = `
* Here is your configuration:
* \`\`\`json
* {"theme": "dark", "fontSize": 14}
* \`\`\`
* Let me know if you need changes.
* `;
* const config = extractJsonFromText(response);
* // Returns: { theme: "dark", fontSize: 14 }
* ```
*/
export declare function extractJsonFromText(text: string): unknown | null;
import type { JsonTypeGuard } from "../../types/index.js";
/**
* Parse JSON from text with optional type validation.
*
* Extracts JSON from text and optionally validates it against a type guard.
* Useful when you need type-safe parsing of AI responses.
*
* @param text - Text that may contain JSON
* @param validator - Optional type guard to validate the parsed result
* @returns Parsed and validated JSON or null if not found/invalid/fails validation
*
* @example
* ```typescript
* interface UserConfig {
* theme: string;
* fontSize: number;
* }
*
* function isUserConfig(obj: unknown): obj is UserConfig {
* return (
* typeof obj === 'object' &&
* obj !== null &&
* 'theme' in obj &&
* 'fontSize' in obj &&
* typeof (obj as UserConfig).theme === 'string' &&
* typeof (obj as UserConfig).fontSize === 'number'
* );
* }
*
* const config = parseJsonFromText<UserConfig>(aiResponse, isUserConfig);
* if (config) {
* // config is typed as UserConfig
* console.log(config.theme, config.fontSize);
* }
* ```
*/
export declare function parseJsonFromText<T>(text: string, validator?: JsonTypeGuard<T>): T | null;
/**
* Extract all JSON objects/arrays from text.
*
* Useful when text contains multiple JSON blocks.
*
* @param text - Text that may contain multiple JSON values
* @returns Array of parsed JSON values
*
* @example
* ```typescript
* const text = 'First: {"a": 1} Second: {"b": 2}';
* const results = extractAllJsonFromText(text);
* // Returns: [{ a: 1 }, { b: 2 }]
* ```
*/
export declare function extractAllJsonFromText(text: string): unknown[];