Skip to content
json2026-07-252 min read

What Is JSON/CSV Conversion

JSON/CSV conversion is the process of converting between JSON arrays (object lists) and CSV table format. JSON suits programmatic processing; CSV suits Excel and analytics tools.

Conversion Example

JSON → CSV:

[
  { "name": "Alice", "age": 28, "city": "Shanghai" },
  { "name": "Bob", "age": 32, "city": "Beijing" }
]

CSV Output:

name,age,city
Alice,28,Shanghai
Bob,32,Beijing

JSON vs CSV Use Cases

| Scenario | JSON | CSV | |----------|------|-----| | Web API | ✅ Preferred | ❌ Not suitable | | Excel processing | ❌ Not native | ✅ Native support | | Nested data | ✅ Supported | ❌ Not supported | | Data analysis | ⚠️ Needs parsing | ✅ Direct analysis | | Data export | ⚠️ Not intuitive | ✅ User-friendly | | Config files | ✅ Suitable | ❌ Not suitable | | Large datasets | ⚠️ Larger files | ✅ Smaller files |

Why You Need JSON/CSV Conversion

  1. Data analysis: Import API JSON data into Excel/Google Sheets
  2. Data export: Export database query results (JSON) as CSV downloads
  3. Data migration: Migrate data between different systems
  4. Report generation: Convert JSON data to CSV for reports
  5. Batch operations: Edit in Excel, export back to JSON
  6. Data visualization: Many tools accept CSV input

Conversion Challenges

Flattening Nested JSON

JSON supports nesting; CSV doesn't. You need to "flatten" nested objects:

// Original
{ "name": "Alice", "address": { "city": "Shanghai", "zip": "200000" } }

// Flattened
{ "name": "Alice", "address.city": "Shanghai", "address.zip": "200000" }

Array Field Handling

JSON arrays need special handling in CSV:

{ "name": "Alice", "tags": ["developer", "admin"] }

Common solutions: join with delimiter (developer;admin) or split into multiple rows.

How to Use an Online Tool

Using DevToolkit Pro's JSON/CSV Converter:

  1. Paste JSON array or CSV data
  2. The tool auto-detects format and converts
  3. Supports nested JSON flattening
  4. Custom delimiters and quote handling
  5. Preview and copy results

Conversion in Code

JavaScript: JSON to CSV

function jsonToCSV(jsonArray) {
  if (!jsonArray.length) return '';
  const flatten = (obj, prefix = '') =>
    Object.entries(obj).reduce((acc, [key, val]) => {
      const fullKey = prefix ? `${prefix}.${key}` : key;
      if (typeof val === 'object' && val !== null && !Array.isArray(val)) {
        Object.assign(acc, flatten(val, fullKey));
      } else {
        acc[fullKey] = Array.isArray(val) ? val.join(';') : val;
      }
      return acc;
    }, {});

  const flatData = jsonArray.map(item => flatten(item));
  const headers = Object.keys(flatData[0]);
  const rows = flatData.map(row =>
    headers.map(h => {
      const val = String(row[h] ?? '');
      return val.includes(',') || val.includes('"') || val.includes('\n')
        ? `"${val.replace(/"/g, '""')}"` : val;
    }).join(',')
  );
  return [headers.join(','), ...rows].join('\n');
}

JavaScript: CSV to JSON

function csvToJSON(csv) {
  const lines = csv.trim().split('\n');
  const headers = lines[0].split(',').map(h => h.trim());
  return lines.slice(1).map(line => {
    const values = line.split(',');
    return headers.reduce((obj, header, i) => {
      let val = values[i]?.trim() ?? '';
      if (val === 'true') val = true;
      else if (val === 'false') val = false;
      else if (val === 'null') val = null;
      else if (!isNaN(val) && val !== '') val = Number(val);
      obj[header] = val;
      return obj;
    }, {});
  });
}

FAQ

How to Convert Nested JSON to CSV?

You need to "flatten" nested objects first. Common approaches: dot notation (address.city) or underscore (address_city). Online tools typically handle flattening automatically.

Why Does CSV to JSON Lose Number Types?

CSV has no type info — all values are strings. Add type inference during conversion: detect numeric strings → Number, true/false → boolean.

How to Handle Nested Arrays in JSON?

JSON arrays can contain other arrays or objects. For CSV conversion, decide: flatten to multiple rows (one per element) or merge into one field (delimiter-separated). Depends on your use case.


This article is brought to you by DevToolkit Pro. More developer tools at the homepage.


ad