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 either JSON or YAML. The function first attempts JSON.parse; on failure it falls back to YAML.load; if both throw, this error is raised before any Swagger/OpenAPI document validation happens. It is a purely input-format error: the bytes are not syntactically valid JSON or YAML.

Source

Thrown at console-ui/src/pages/AI/services/OpenApiService.js:73

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

// 校验格式并解析 OpenAPI
export const parseOpenAPI = async content => {
    try {
        // 自动识别 JSON/YAML 格式
        let parsedContent;
        try {
            parsedContent = JSON.parse(content);
        } catch (jsonError) {
            // 尝试 YAML 解析
            try {
                parsedContent = YAML.load(content);
            } catch (yamlError) {
                throw new Error('Invalid JSON/YAML format');
            }
        }
        parsedContent = resolveRefs(parsedContent, parsedContent);
        if (parsedContent.swagger) {
            const converted = await swagger2openapi.convertObj(parsedContent, {});
            return converted.openapi;
        }

        // 验证 OpenAPI 3.x 文档
        if (parsedContent.openapi) {
            // 可以添加更多验证逻辑
            return parsedContent;
        }
    } catch (e) {
        console.error('解析失败:', e);
        throw new Error('File format invalid');
    }
};

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Validate the content with an external JSON/YAML linter (e.g. jsonlint.com or yamllint) before importing.
  2. Ensure the input is a plain UTF-8 string with no smart quotes, non-breaking spaces, or BOM prefix; re-paste as plain text.
  3. If pasting from a browser/IDE, disable rich-text paste or pipe through a sanitizer that strips zero-width characters.
  4. Add a pre-check that strips a UTF-8 BOM and normalizes whitespace before calling parseOpenAPI.

Example fix

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

// after
const clean = content.replace(/^\uFEFF/, '').replace(/[\u200B-\u200D\uFEFF]/g, '');
const doc = await parseOpenAPI(clean);
Defensive patterns

Strategy: validation

Validate before calling

function tryParse(content) {
  try { JSON.parse(content); return { ok: true, format: 'json' }; } catch {}
  try { YAML.load(content); return { ok: true, format: 'yaml' }; } catch (e) { return { ok: false, error: e }; }
}
const check = tryParse(raw);
if (!check.ok) { showToast('Content is neither valid JSON nor YAML'); }

Type guard

function isParsable(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(cleanContent);
} catch (e) {
  if (/Invalid JSON\/YAML format/.test(e.message)) { alert('Please paste valid JSON or YAML.'); }
  else { throw e; }
}

Prevention

When it happens

Trigger: A user pastes an OpenAPI document into the AI service tool importer with a stray trailing comma, mismatched brace, or unquoted key (invalid JSON) AND a tab-based indentation / special-character problem (invalid YAML). Copy-paste from a rich-text editor can inject non-breaking spaces or smart quotes that break both parsers.

Common situations: Importing a Swagger/OpenAPI spec file that was corrupted during copy-paste, downloaded with HTML entities, or encoded in a non-UTF-8 charset. Empty content string passed from a textarea. Content truncated by a file upload size limit, leaving half a JSON object.

Understand the failure class

Related errors


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