JSON vs CSV
JSON vs CSV — Quick Summary
| JSON | CSV | |
|---|---|---|
| Structure | Hierarchical — nested objects and arrays | Flat — rows and columns only |
| Data types | string, number, boolean, null, object, array | Everything is a string (parsed by convention) |
| Nested data | Native support | Not supported — must flatten or encode |
| Null values | null keyword | Empty cell or convention (e.g. "NULL") |
| Schema | JSON Schema | No standard schema — convention only |
| Human readable | Yes — with formatting | Yes — spreadsheet-friendly |
| File size | Larger for tabular data (field names repeat per row in JSON array) | Smaller for tabular data |
| Streaming | Complex — requires full parse or NDJSON | Easy — line by line |
| Spreadsheet import | Not native — requires conversion | Native — Excel, Sheets, LibreOffice |
| Database import | Most NoSQL, some SQL via JSON functions | Universal — LOAD DATA, COPY, etc. |
| Best for | APIs, config, nested/hierarchical data | Reports, exports, data science, ETL |
Side-by-Side Example
An array of user records in both formats:
json
// JSON — self-describing, nested-friendly
[
{ "id": 1, "name": "Ravi Kumar", "city": "Surat", "active": true, "score": 9.5 },
{ "id": 2, "name": "Priya Singh", "city": "Mumbai", "active": false, "score": 7.2 },
{ "id": 3, "name": "Dev Mehta", "city": "Pune", "active": true, "score": 8.8 }
]text
# CSV — compact for tabular data, no nesting
id,name,city,active,score
1,Ravi Kumar,Surat,true,9.5
2,Priya Singh,Mumbai,false,7.2
3,Dev Mehta,Pune,true,8.8CSV is more compact for flat tabular data. JSON becomes necessary when records have nested fields.
Parsing in Go
go
// JSON — standard library
import "encoding/json"
type User struct {
ID int `json:"id"`
Name string `json:"name"`
City string `json:"city"`
Active bool `json:"active"`
Score float64 `json:"score"`
}
var users []User
err := json.Unmarshal(data, &users)
// CSV — standard library
import (
"encoding/csv"
"strconv"
"strings"
)
r := csv.NewReader(strings.NewReader(csvData))
records, err := r.ReadAll()
// records[0] = ["id","name","city","active","score"] (header)
// records[1] = ["1","Ravi Kumar","Surat","true","9.5"]
// Parse manually or use gocsv:
// go get github.com/gocarina/gocsv
import "github.com/gocarina/gocsv"
var users2 []User
gocsv.UnmarshalString(csvData, &users2)When JSON beats CSV
- Nested data — a user with an embedded address, orders, or preferences. CSV cannot represent this without multiple files or encoding tricks.
- Type fidelity — JSON preserves the distinction between the number 42 and the string "42", or between false and "false".
- API responses — HTTP APIs almost universally return JSON, not CSV.
- Variable schema — different records can have different fields. CSV assumes all rows have the same columns.
When CSV beats JSON
- Spreadsheets — business users expect CSV exports. Excel and Google Sheets open CSV natively.
- Bulk database imports — LOAD DATA INFILE, PostgreSQL COPY, BigQuery load jobs all handle CSV optimally.
- Data science — pandas, R, NumPy all read CSV with one line. JSON requires more parsing.
- File size — for flat tabular data with many records, CSV is significantly smaller than JSON because field names are not repeated per record.
- Streaming line-by-line — CSV streams naturally. You can process row-by-row without loading the entire file.