Last week our team built a pipeline that fed structured data from ChatGPT responses directly into our microservices. The idea was genius: ask the AI to generate test fixtures in JSON, parse them, and feed them into the test runner. In theory, no more manual fixture writing.

In practice, our Slack channel turned into a graveyard of JSON.parse failures.

The problem wasn't the AI. The problem was that AI tools write JSON the way humans think JSON works, not how JSON.parse() actually expects it. Let me walk through the patterns I've seen, and how to fix each one.

The Markdown Code Fence Trap

This is the most common issue. You ask ChatGPT "give me JSON for a user object" and it responds with:

```json
{
  "name": "Alice",
  "email": "alice@example.com"
}
```

That triple backtick block is Markdown, not JSON. If you pipe the raw response to JSON.parse(), you get a position 0 error because the first character is the backtick.

// Don't do this โ€” raw response has markdown
const raw = response.data; // "```json\n{\"name\":...}\n```"
JSON.parse(raw); // ๐Ÿ’ฅ

// Do this โ€” strip the code fences
function extractJsonFromMarkdown(text) {
  const match = text.match(/```(?:json)?\n([\s\S]*?)\n```/);
  return match ? match[1].trim() : text.trim();
}

const jsonStr = extractJsonFromMarkdown(raw);
JSON.parse(jsonStr); // โœ…

Pro tip: Some AI APIs let you set a response_format parameter to { "type": "json_object" }. Use it. OpenAI's API supports this natively, which forces the model to output pure JSON without markdown wrapping.

Truncated Output and Mid-Structure Breaks

AI tools have token limits. When you ask for a large JSON structure โ€” say, 200 user records โ€” the model might hit its limit and stop mid-object:

[
  {
    "id": 1,
    "name": "Alice",
    "tags": ["admin", "premium", "beta-tes

That's incomplete JSON. The array never closes, strings are cut off, and commas are missing.

How to detect truncation: Use a JSON validator that shows you line numbers. The JSON Formatter will highlight the exact position where parsing stops, so you can tell if it's a truncation issue or a syntax error.

How to fix it:

// Check if the response ends with a valid JSON terminator
function isProbablyComplete(str) {
  const trimmed = str.trim();
  const last = trimmed[trimmed.length - 1];
  return last === '}' || last === ']' || last === '"';
}

If it's truncated, split the generation into smaller chunks. Instead of "generate 200 users", try "generate 20 users, repeat 10 times."

Hallucinated or Extra Fields

AI models sometimes invent fields that don't match your schema. One week ChatGPT started adding a "user_type": "standard" field to every user object โ€” a field our system had never seen before.

{
  "id": 1,
  "name": "Alice",
  "user_type": "standard", // Where did this come from?
  "department": 42
}

This won't crash JSON.parse(), but it will crash your downstream code if you don't validate the schema first.

My approach: Run AI-generated JSON through a validator that strips unknown fields or flags them. The tree view in JSON Formatter helps you spot unexpected keys at a glance.

Encoding and Smart Quote Issues

AI tools love to use typographic quotes and em-dashes in their output. When you copy JSON from ChatGPT's web interface, the quotes get "curled" during copy-paste:

{
  "name": "Alice",
  "description": "She said \u201Chello\u201D" // Smart quotes!
}

Parse that and you're done. The \u201C and \u201D are valid Unicode but not valid JSON string delimiters.

Fix: always run AI-generated JSON through a character sanitizer before parsing.

function sanitizeAIJson(text) {
  // Fix smart quotes
  let s = text
    .replace(/[\u201C\u201D]/g, '"')
    .replace(/[\u2018\u2019]/g, "'")
    .replace(/[\u2013\u2014]/g, '-');
  // Fix non-printable chars (AI models sometimes leak these)
  s = s.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '');
  return s;
}

If you're working with pasted AI output rather than an API, paste the raw text into the JSON Formatter first and run validation. It'll flag any encoding issues in the error panel.

Mixed Data Types in Arrays

Another AI quirk: the model might output a heterogeneous array when you expect a uniform one.

{
  "items": [
    { "id": 1, "value": "string" },
    { "id": 2, "value": 42 },       // Was supposed to be a string
    null,                            // Where did this come from?
    "unexpected string at array level"
  ]
}

This parses fine but breaks your type assumptions. Always validate the shape of AI-generated JSON, not just whether it parses.

My Production Pipeline

Here's what I ended up building after weeks of trial and error:

class AIJsonValidator {
  async validate(raw) {
    const extracted = this.extractJson(raw);
    const sanitized = this.sanitize(sanitizeAIJson(extracted));
    try {
      const data = JSON.parse(sanitized);
      return { valid: true, data };
    } catch (e) {
      return { 
        valid: false, 
        error: e.message,
        position: e.message.match(/position (\d+)/)?.[1]
      };
    }
  }
  
  extractJson(text) {
    const match = text.match(/```(?:json)?\n([\s\S]*?)(?:\n```|$)/);
    return match ? match[1].trim() : text.trim();
  }
}

Before feeding any AI output into production, I paste a sample into the JSON Formatter and check the tree view. It catches about 90% of the weirdness before it hits my pipeline.

FAQ

Q: Does setting response_format to json_object guarantee valid JSON?

A: Almost always. It tells the model to output pure JSON without markdown, but I've still seen rare cases of truncated output from token limits.

Q: Why does ChatGPT sometimes wrap JSON in code blocks even when I say "only output JSON"?

A: The model's training data includes Markdown formatting examples. Adding explicit system instructions like "Output raw JSON only, no markdown formatting" helps.

Q: Can I validate AI JSON without an API call?

A: Yes. Copy the output and paste it into the JSON Formatter. The "Validate" and "Tree View" tabs show you exactly what's wrong.

Q: How do I handle AI models that sometimes output JSON with trailing commas?

A: Check out our guide on trailing commas and single quotes in non-standard JSON โ€” it covers exactly this edge case.

Q: Should I use JSON.parse or a stricter parser for AI output?

A: JSON.parse is fine, but always wrap it in try-catch with a fallback. The AI might output something that parses correctly but has wrong structure.

Q: What about AI tools that output YAML instead of JSON?

A: The JSON Formatter has a JSON-to-YAML converter. Convert it first, then work with YAML.

Q: Can I use streaming with AI JSON generation to catch truncation early?

A: Yes, but you'd need to buffer the stream and validate the final complete response. Partial JSON is almost always invalid.