Parsing JSON Responses from Gemini AI
Gemini often wraps JSON responses in markdown code blocks that need to be stripped.
Problem
Gemini responses may look like:
Here's the translation:
```json
[
{"original": "日本語", "translated": "Japanese"}
]
```
Solution: Strip Markdown Code Blocks
function parseGeminiJSON(text: string): any {
// Remove markdown code blocks with optional language identifier
const cleaned = text.replace(/```(?:json)?\s*([\s\S]*?)\s*```/, '$1');
// Parse the cleaned JSON
return JSON.parse(cleaned.trim());
}
Regex Breakdown
```- Matches opening code block (triple backticks)(?:json)?- Optional "json" language identifier\s*- Optional whitespace([\s\S]*?)- Captures content (non-greedy)\s*- Optional whitespace```- Matches closing code block
Robust Version
function parseGeminiJSON(text: string): any {
let cleaned = text;
// Remove markdown code blocks
cleaned = cleaned.replace(/```(?:json)?\s*([\s\S]*?)\s*```/, '$1');
// Remove any text before/after JSON
const jsonMatch = cleaned.match(/\[[\s\S]*\]|\{[\s\S]*\}/);
if (jsonMatch) {
cleaned = jsonMatch[0];
}
return JSON.parse(cleaned.trim());
}
Usage Example
const response = await model.generateContent(prompt);
const rawText = response.response.text();
try {
const parsed = parseGeminiJSON(rawText);
console.log(parsed);
} catch (error) {
console.error('Failed to parse JSON:', rawText);
}
Common Patterns
| Input Pattern | Output |
|---|---|
```json\n{...}\n``` |
{...} |
```\n{...}\n``` |
{...} |
Here's the result:\n```json\n{...}\n``` |
{...} |
Some text\n{...}\nMore text |
{...} |