Try it yourself with our free Json To Csv tool — runs entirely in your browser, no signup needed.

How to Convert JSON to CSV for Data Migration

How to Convert JSON to CSV for Data Migration

When migrating data from one system to another, it's common to encounter data in JSON format that needs to be converted to CSV for further processing or import into a different system. This can be a challenging task, especially when dealing with large datasets or complex JSON structures. In this guide, we'll explore the process of converting JSON to CSV for data migration, providing practical examples, best practices, and common mistakes to avoid.

Quick Example

Here's a minimal example in JavaScript using the json2csv library to convert a simple JSON object to CSV:

import json2csv from 'json2csv';

const jsonData = [
  { name: 'John', age: 30, city: 'New York' },
  { name: 'Jane', age: 25, city: 'London' }
];

const csv = json2csv.parse(jsonData);
console.log(csv);
// Output:
// "name","age","city"
// "John",30,"New York"
// "Jane",25,"London"

// Install json2csv using npm or yarn:
// npm install json2csv
// yarn add json2csv

Real-World Scenarios

Scenario 1: Converting Nested JSON Objects

When dealing with nested JSON objects, we need to flatten the data to convert it to CSV. We can use the json2csv library's flatten option to achieve this.

import json2csv from 'json2csv';

const jsonData = [
  {
    name: 'John',
    address: {
      street: '123 Main St',
      city: 'New York'
    }
  },
  {
    name: 'Jane',
    address: {
      street: '456 Elm St',
      city: 'London'
    }
  }
];

const csv = json2csv.parse(jsonData, { flatten: true });
console.log(csv);
// Output:
// "name","address.street","address.city"
// "John","123 Main St","New York"
// "Jane","456 Elm St","London"

Scenario 2: Handling Arrays in JSON

When JSON data contains arrays, we need to decide how to represent them in the CSV output. We can use the json2csv library's arraySeparator option to specify a separator for array values.

import json2csv from 'json2csv';

const jsonData = [
  {
    name: 'John',
    interests: ['reading', 'hiking', 'coding']
  },
  {
    name: 'Jane',
    interests: ['music', 'art', 'cooking']
  }
];

const csv = json2csv.parse(jsonData, { arraySeparator: '|' });
console.log(csv);
// Output:
// "name","interests"
// "John","reading|hiking|coding"
// "Jane","music|art|cooking"

Scenario 3: Converting JSON with Dates

When working with JSON data that contains dates, we need to ensure that the dates are formatted correctly for the CSV output. We can use the json2csv library's dateFormat option to specify a date format.

import json2csv from 'json2csv';

const jsonData = [
  {
    name: 'John',
    birthdate: new Date('1990-01-01T00:00:00.000Z')
  },
  {
    name: 'Jane',
    birthdate: new Date('1995-06-01T00:00:00.000Z')
  }
];

const csv = json2csv.parse(jsonData, { dateFormat: 'YYYY-MM-DD' });
console.log(csv);
// Output:
// "name","birthdate"
// "John","1990-01-01"
// "Jane","1995-06-01"

Best Practices

  1. Validate JSON data: Before converting JSON to CSV, ensure that the data is valid and well-formed to avoid errors.
  2. Use a library: Utilize a reputable library like json2csv to simplify the conversion process and handle edge cases.
  3. Specify options: Use library options to customize the conversion process, such as specifying a date format or array separator.
  4. Test thoroughly: Verify that the converted CSV data is accurate and complete.
  5. Document the process: Keep a record of the conversion process, including any options used and potential issues encountered.

Common Mistakes

Mistake 1: Incorrect JSON parsing

Wrong code:

const jsonData = JSON.parse('{ "name": "John" }');
const csv = json2csv.parse(jsonData);

Corrected code:

const jsonData = JSON.parse('{ "name": "John" }');
const csv = json2csv.parse([jsonData]); // Pass an array of objects

Mistake 2: Insufficient error handling

Wrong code:

try {
  const csv = json2csv.parse(jsonData);
} catch (error) {
  console.log('Error occurred');
}

Corrected code:

try {
  const csv = json2csv.parse(jsonData);
} catch (error) {
  console.error('Error occurred:', error.message);
  // Handle the error or rethrow it
}

Mistake 3: Ignoring data types

Wrong code:

const jsonData = [
  { name: 'John', age: '30' }
];
const csv = json2csv.parse(jsonData);

Corrected code:

const jsonData = [
  { name: 'John', age: 30 } // Ensure age is a number
];
const csv = json2csv.parse(jsonData);

FAQ

Q: Can I convert JSON to CSV without using a library?

A: While it's possible to write custom code to convert JSON to CSV, using a library like json2csv is recommended to simplify the process and handle edge cases.

Q: How do I handle nested JSON objects?

A: Use the flatten option with the json2csv library to flatten nested objects.

Q: Can I specify a custom date format?

A: Yes, use the dateFormat option with the json2csv library to specify a custom date format.

Q: What if my JSON data contains arrays?

A: Use the arraySeparator option with the json2csv library to specify a separator for array values.

Q: How do I ensure accurate CSV output?

A: Verify that the converted CSV data is accurate and complete by testing thoroughly.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp