Which format to choose
Choose the format by what will read the output.
- CSV suits spreadsheets, database imports and anything that expects flat rows and columns. It works best for arrays of similar objects.
- YAML suits configuration files and anything people will read and edit by hand. It keeps nesting and arrays intact.
- XML suits older systems, feeds and tools that expect a root element and tags. This converter writes elements only, never attributes.
How to use the tool
- Open the JSON to CSV, JSON to YAML or JSON to XML tab.
- Paste your JSON into the input. The page opens with an example array, and pasting replaces it.
- Read the output, which updates as you type. If the JSON has a syntax error, the tool shows the line and column.
- Set any option you need: the separator for CSV (comma, semicolon or tab), or the root element name for XML.
- Select Copy or Download.
Everything runs in your browser. Nothing is uploaded.
How nested data is handled in CSV
An array of objects becomes one row per object. The header is the union of all keys, in the order each first appears. Nested objects are flattened into dotted columns such as address.city. Arrays inside objects are written as JSON text in a single cell, and null becomes an empty cell.
[
{ "name": "Ada", "address": { "city": "London" }, "tags": ["a", "b"] },
{ "name": "Grace", "address": { "city": "New York" }, "tags": null }
]
name,address.city,tags
Ada,London,"[""a"",""b""]"
Grace,New York,
A top-level array of arrays is written as rows directly, and a single object becomes one row. Quoting follows RFC 4180: a cell containing the separator, a quote or a line break is wrapped in double quotes, and inner quotes are doubled. Lines end with CRLF.
If Excel in your locale uses a decimal comma, choose the semicolon separator.
YAML quoting and the Norway problem
A string stays unquoted only when YAML would read it back as the same string. Otherwise it is double-quoted. This matters because YAML 1.1 parsers read yes, no, on and off as booleans. The best-known case is the country code NO, which such parsers turn into false. That is the "Norway problem".
{ "answer": "yes", "switch": "on", "nothing": "null", "version": "1.0", "note": "a: b", "empty": "" }
answer: "yes"
switch: "on"
nothing: "null"
version: "1.0"
note: "a: b"
empty: ""
Empty objects and arrays are written as {} and []. The output is tested to read back as the same data in a YAML parser.
How XML elements are named
Each key becomes an element. Array items repeat the element, so "roles": ["admin", "editor"] gives two <roles> elements. A top-level array is wrapped in a root element, named root by default, with <item> children. A null value becomes an empty element such as <x/>.
Keys that are not valid XML names are adjusted:
- Spaces become
_. - A leading digit gets a
_prefix. - A name starting with
xmlgets a_prefix.
<first_name>Ada</first_name>
<roles>admin</roles>
<roles>editor</roles>
<_1st/>
Text is escaped, so &, <, > and " become &, <, > and ".
Large numbers
Numbers are copied exactly as written. An integer such as 12345678901234567890 is not rounded, although JSON.parse would round it.
Converting in code
For CSV from an array of objects with jq, the header comes from the first object's keys, so this suits flat objects that share the same keys:
jq -r '(.[0] | keys_unsorted) as $k | $k, (.[] | [.[$k[]]]) | @csv' data.json
For YAML with Python (this needs PyYAML):
python -c 'import sys, json, yaml; print(yaml.safe_dump(json.load(sys.stdin), sort_keys=False))' < data.json
Or with mikefarah's yq v4:
yq -P data.json