alibaba/nacos · error · Error

Invalid JSON/YAML format

Error message

Invalid JSON/YAML format

What it means

Thrown by parseOpenAPI() when the supplied content cannot be parsed as JSON (JSON.parse throws) AND cannot be parsed as YAML (YAML.load throws). The function tries JSON first, then falls back to YAML; only when both fail does it throw 'Invalid JSON/YAML format'. This is a content-level parse failure, before any OpenAPI structural validation.

Source

Thrown at console-ui-next/src/utils/openapi/parseOpenApi.ts:62

  for (const [key, value] of Object.entries(obj)) {
    result[key] = resolveRefs(value, root, visited);
  }
  return result;
}

/**
 * Parse OpenAPI/Swagger content (JSON or YAML) and return an OpenAPI 3.x document.
 */
export async function parseOpenAPI(content: string): Promise<any> {
  let parsed: any;

  try {
    parsed = JSON.parse(content);
  } catch {
    try {
      parsed = YAML.load(content);
    } catch {
      throw new Error('Invalid JSON/YAML format');
    }
  }

  parsed = resolveRefs(parsed, parsed);

  // Swagger 2.x -> OpenAPI 3.x
  if (parsed.swagger) {
    const converted = await swagger2openapi.convertObj(parsed, {});
    return converted.openapi;
  }

  if (parsed.openapi) {
    return parsed;
  }

  throw new Error('File format invalid: not a valid OpenAPI or Swagger document');
}

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Validate the file is actually an OpenAPI/Swagger spec before uploading.
  2. Run the content through a JSON/YAML linter to find the syntax error.
  3. For YAML, replace tabs with spaces and check indentation.
  4. Catch the error and prompt the user to re-upload a corrected file.

Example fix

// before
const doc = await parseOpenAPI(content);

// after
try {
  const doc = await parseOpenAPI(content);
} catch (e) {
  if (e.message === 'Invalid JSON/YAML format') {
    throw new Error('The uploaded file is not valid JSON or YAML. Please check the syntax and try again.');
  }
  throw e;
}
Defensive patterns

Strategy: validation

Validate before calling

function isParsableSpec(content: string): boolean {
  try { JSON.parse(content); return true; } catch {}
  try { YAML.load(content); return true; } catch {}
  return false;
}

Try / catch

try {
  const doc = await parseOpenAPI(content);
} catch (e) {
  if (e.message === 'Invalid JSON/YAML format') {
    setError('The file is not valid JSON or YAML. Check syntax and re-upload.');
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing a string that is neither valid JSON nor valid YAML — e.g. plain prose, a fragment of code, a binary/base64 blob, or a file with syntax errors (unbalanced braces, bad indentation, stray tabs). Reached via the OpenAPI import flow in the console.

Common situations: User uploads a non-spec file (README, CSV, screenshot-dump text). YAML with tab indentation (YAML forbids tabs). Truncated/corrupted download. JSON with trailing commas or single quotes.

Understand the failure class

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/2400c2ab61022d5d. Report an issue: GitHub.