JSON and CSV are two very common and popular text based data formats. But what is the difference between these two exactly? Which one should you pick to store your data? This article lists the key differences of JSON vs CSV and shows examples to get a feel for it. In the end we discuss when it is best to use JSON and when to use CSV.
Key differences between JSON and CSV
Property | JSON | CSV |
---|---|---|
Full name | JavaScript Object Notation | Comma Separated Values |
Data structures | Any nested data structure | Tabular data |
Data size | Verbose | Compact |
Data types | Object, array, string, number, boolean, null | String |
Typical applications | Data exchange between applications, NoSQL databases, configuration files | Spreadsheets, datasets in data science applications |
Flexibility | Flexible: any nested structure | Limited: tabular data only |
Performance | High | High |
Ubiquity | High | High |
Specification | https://json.org/ | https://datatracker.ietf.org/doc/html/rfc4180 |
A note about the data size: in real world data sets, a JSON file is typically 1.5 to 3 times as large as CSV. However, when zipping the files, the difference is typically only 10% or 20%, since a zip algorithm can very efficiently deal with whitespacing and the duplication of keys in a JSON file.
The performance of both CSV and JSON is marked “high”. Of course the performance differs, and this depends on the used language, library, and your particular data structure. CSV may be a bit faster because it is a more compact format. However, looking from a high level perspective we can see that both formats are lightweight text formats and their performance has the same order of magnitude.
Comparing JSON and CSV by example
The easiest way to get an idea of how JSON and CSV differ is to look at an example that formats a list with people each having a name, age, and city.
An example of JSON data. It basically consists of lists [...]
containing comma separated items, and objects {...}
, and objects contain key value pairs.
[
{ "name": "Chris", "age": 23, "city": "New York" },
{ "name": "Emily", "age": 19, "city": "Atlanta" },
{ "name": "Joe", "age": 32, "city": "New York" },
{ "name": "Kevin", "age": 19, "city": "Atlanta" },
{ "name": "Michelle", "age": 27, "city": "Los Angeles" },
{ "name": "Robert", "age": 45, "city": "Manhattan" },
{ "name": "Sarah", "age": 31, "city": "New York" }
]
And the same data in CSV. The data can contain an optional header with column names on the first line, followed by rows with comma separated values.
name,age,city
Chris,23,New York
Emily,19,Atlanta
Joe,32,New York
Kevin,19,Atlanta
Michelle,27,Los Angeles
Robert,45,Manhattan
Sarah,31,New York
A more rich example of JSON data the following object containing nested objects and arrays:
{
"name": "Sarah",
"nickname": null,
"hobbies": ["swimming", "reading", "movies"],
"friends": [
{ "name": "Joe", "nickname": "Big Joe" },
{ "name": "Emily", "nickname": null }
],
"active": true
}
This data with nested structures cannot be represented in CSV, because it is not a list with records.
It is possible though to represent a list with nested objects in CSV. For example the following JSON data:
[
{
"name": "Chris",
"address": {
"city": "New York",
"street": "Park Avenue"
}
},
{
"name": "Emily",
"address": {
"city": "Atlanta",
"street": "Peachtree Street"
}
}
]
Can be represented in CSV as follows, where we encode the city and street properties which are nested inside the object address using a dot notation in the header: “address.city” and “address.street”.
name,address.city,address.street
Chris,New York,Park Avenue
Emily,Atlanta,Peachtree Street
Should I use JSON or CSV?
You may be tempted to choose JSON or CSV based on how compact or fast the data format is, but in practice that is hardly ever the most important. Best is to look at what you need to do with the data:
- Do you have tabular data and need to edit this in a spreadsheet application, print charts, and calculate statistics? Use CSV.
- Do you need flexible data structures and interact with REST APIs or NoSQL databases? Use JSON
Can I change JSON to CSV?
Yes, you can convert JSON to CSV and the other way around, but only when the data is tabular. There are many libraries and applications that can do the conversion. You can read all about that in the article “Convert JSON to CSV using JSON Editor Online”.
Conclusion about JSON vs CSV
So, is JSON better than CSV? Both formats are mainstream, simple, and universal. They are simply different, and which one is best depends on the environment where you want to use your data.