Oh MyUtils

JSON转YAML转换器 - 双向转换 在线

JSON和YAML格式间即时转换,带实时验证。适用于Kubernetes配置、Docker Compose和CI/CD管道。

方向
缩进
排序键
输入 (JSON)
输出 (YAML)
 

常见问题

What is a JSON-YAML converter?

A JSON-YAML converter is a tool that transforms data between JSON (JavaScript Object Notation) and YAML (YAML Ain't Markup Language) formats. JSON uses braces, brackets, and double quotes for structure, while YAML uses indentation and minimal punctuation for a more human-readable format. Both represent the same data structures (objects/mappings, arrays/sequences, and scalar values), making lossless bidirectional conversion possible.

How do I use this JSON-YAML converter?

1. Select your conversion direction using the tabs: JSON to YAML or YAML to JSON. 2. Paste your data into the input area. 3. The conversion happens automatically in real-time as you type. 4. Review the converted output. 5. Adjust formatting options as needed. 6. Click Copy to copy the result. 7. Use Download to save the output as a file.

Is my data safe?

Your data is 100% safe and never leaves your browser. This converter uses the js-yaml library running entirely in client-side JavaScript. No data is transmitted to any server. All parsing, validation, and conversion happens locally on your device.

What happens to YAML comments during conversion?

YAML comments (lines starting with #) are lost when converting YAML to JSON because JSON does not support comments. This is a fundamental limitation of the JSON format. The data values themselves are always preserved accurately during conversion.

What YAML features are supported?

This converter supports the YAML 1.2 specification including mappings, sequences, scalar values, multi-line strings, nested structures, and quoted strings. Basic anchor and alias resolution is supported.

Why might my YAML-to-JSON conversion fail?

Common reasons include incorrect indentation, mixing tabs and spaces, unquoted special characters in strings, duplicate keys, and invalid multi-line string indicators. The error message shows the exact line and column where the issue was detected.

What is the difference between JSON and YAML?

JSON uses braces for objects, brackets for arrays, requires double-quoted keys, and does not support comments. YAML uses indentation for structure, supports comments with #, and has features like multi-line strings. JSON is common in APIs, YAML is common in configuration files like Kubernetes and Docker Compose.

代码示例

// JSON to YAML conversion using js-yaml library
// Install: npm install js-yaml
const yaml = require('js-yaml');

function jsonToYaml(jsonString, options = {}) {
  const { indent = 2, sortKeys = false } = options;
  try {
    const parsed = JSON.parse(jsonString);
    return yaml.dump(parsed, { indent, sortKeys, lineWidth: -1 });
  } catch (error) {
    return `Error: ${error.message}`;
  }
}

function yamlToJson(yamlString, options = {}) {
  const { indent = 2, sortKeys = false } = options;
  try {
    const parsed = yaml.load(yamlString);
    return JSON.stringify(parsed, null, indent);
  } catch (error) {
    return `Error: ${error.message}`;
  }
}

// Example
const json = '{"name": "app", "version": "1.0", "debug": true}';
console.log(jsonToYaml(json));
// name: app
// version: '1.0'
// debug: true

相关工具