Class JsonText
Some AI providers may return textual responses that contain additional commentary or formatting around the actual JSON payload requested by the application. This helper provides a minimal extraction mechanism that isolates the first JSON object found within such responses so that it can be deserialized safely.
The current implementation performs a simple structural search for the first
opening brace ({) and the last closing brace (}),
and returns the substring spanning those positions.
The method is intentionally tolerant of provider-specific output formats and is primarily used as a defensive measure to recover valid JSON payloads from otherwise well-formed responses.
This class is a non-instantiable utility holder.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringextractFirstJsonObject(String text) Extracts the first JSON object found within a text response.
-
Method Details
-
extractFirstJsonObject
Extracts the first JSON object found within a text response.The method scans the supplied text for the first occurrence of an opening brace (
{) and the last occurrence of a closing brace (}). The substring between these positions (inclusive) is returned as the extracted JSON object.This approach allows the application to recover structured data even when the model returns additional natural-language content or formatting around the JSON payload.
- Parameters:
text- text returned by the AI model- Returns:
- extracted JSON object as text
- Throws:
AiSuggestionException- if the input text is empty or if no valid JSON object boundaries can be located
-