CSV 转 JSON 转换器

这个 CSV 转 JSON 转换器可将逗号分隔值数据转换为 JSON 格式。粘贴 CSV 数据或上传文件,即可获得格式正确的 JSON 输出,支持自动检测标题、推断数据类型及自定义分隔符。

CSV to JSON Converter

Convert CSV data to JSON format

Frequently Asked Questions

如何将CSV转换为JSON?

将CSV转换为JSON时,CSV数据的第一行用作键(字段名),后续每行成为以这些键映射到行值的JSON对象。结果是JSON对象数组。例如,有"name,age"标题和"Alice,30"行的CSV变为[{"name":"Alice","age":"30"}]。我们的工具会自动处理此过程,包括类型推断。

CSV和JSON有什么区别?

CSV(逗号分隔值)是一种平坦的表格格式,数据按行和列组织,类似于电子表格。JSON(JavaScript对象表示法)是一种层次化格式,支持嵌套对象、数组和类型值(字符串、数字、布尔值、null)。CSV对于平面数据更简单、更小,而JSON对于复杂的嵌套结构更灵活。

CSV可以像JSON一样有嵌套数据吗?

CSV本身不支持嵌套数据结构,它本质上是平面的行列结构。要在CSV中表示嵌套数据,常见的解决方法包括在标题中使用点符号(如"address.city")、在单元格中使用JSON字符串,或使用多个相关CSV文件。在CSV转JSON时,某些工具可以将点符号标题解释为嵌套对象。

CSV标题在JSON转换中如何处理?

CSV标题(第一行)成为每个JSON对象中的属性键。后续每行创建一个新对象,其中标题值为键,单元格值为对应值。如果标题包含空格或特殊字符,它们在JSON键中原样保留。根据转换器的不同,重复的标题可能导致数据丢失或覆盖。

如何处理CSV字段内的逗号?

根据CSV规范(RFC 4180),包含逗号的字段必须用双引号括起来。例如:"John,"New York, NY",30"正确地将"New York, NY"视为单个字段。如果字段包含双引号,则通过将其加倍来转义:"He said ""hello"""。我们的转换器正确处理带引号的字段和转义字符。

什么是JSON数组与JSON对象?

JSON数组是用方括号括起来的有序值列表:[1, 2, 3]。JSON对象是用大括号括起来的键值对无序集合:{"name": "Alice"}。将CSV转换为JSON时,典型输出是对象数组,其中每个对象代表一行数据,列标题作为键。

API使用哪种格式更好,CSV还是JSON?

JSON是现代API的主流格式,因为它支持复杂数据类型、嵌套结构,并且可以被JavaScript原生解析。CSV更适合批量数据导出、电子表格导入和数据分析工具。REST API几乎普遍使用JSON,而CSV常见于数据管道、报告工具和数据库导入/导出操作。

CSV to JSON Converter

About the CSV to JSON Converter

The CSV to JSON Converter transforms CSV (Comma-Separated Values) data into JSON format. JSON is the standard data format for web APIs, configuration files, and modern applications. This tool parses CSV text and produces a well-formatted JSON array of objects, using the first row as property names when headers are enabled.

How to Use

  1. Paste your CSV data into the input field
  2. Select the delimiter used in your CSV (comma, tab, or semicolon)
  3. Toggle whether the first row contains headers
  4. The JSON output is generated instantly as you type
  5. Copy the result to your clipboard with one click

Features

  • Real-time conversion as you type
  • Support for quoted fields containing delimiters, newlines, and escaped quotes
  • Multiple delimiter options: comma, tab, semicolon
  • Optional first-row-as-headers mode for named properties
  • Pretty-printed JSON output with proper indentation
  • One-click copy to clipboard

CSV Format Details

The converter handles standard CSV formatting rules:

  • Fields are separated by the chosen delimiter
  • Fields containing the delimiter, quotes, or newlines must be enclosed in double quotes
  • Double quotes within quoted fields are escaped by doubling them ("")
  • Empty lines are skipped automatically

Example input and output:

Input CSV:
name,age,city
"Smith, John",30,"New York"
Jane,25,London

Output JSON:
[
  { "name": "Smith, John", "age": "30", "city": "New York" },
  { "name": "Jane", "age": "25", "city": "London" }
]

Common Use Cases

  • Importing spreadsheet data into web applications
  • Preparing data for API requests
  • Converting exported database data to JSON format
  • Transforming log files for analysis tools
  • Data migration between CSV-based and JSON-based systems