← Back to Home

How to Parse JSON Responses from Gemini AI

Updated January 14, 2026
geminijson parsingmarkdown code blocksresponse handling

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

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 {...}