Convert CSV to JSON — and open Bangla CSV in Excel
Someone exports a customer list from an admin panel and sends it over. You need it as JSON for an API import — and your manager needs to open it in Excel. Two things go wrong again and again: the conversion quietly damages data (phone numbers lose their leading 0, a comma inside an address shifts every column), and Excel shows the Bangla names as রহিম instead of রহিম. This guide explains why both happen and how to fix them for good.
What a CSV file really is
CSV (“comma-separated values”) looks too simple to need rules, which is exactly why people get it wrong. The closest thing to a standard is RFC 4180:
- One record per line; lines end with CRLF (
\r\n), though most readers also accept\n. - Fields are separated by commas. The first line is often, but not always, a header with column names.
- A field that contains a comma, a double quote or a line break must be wrapped in double quotes.
- A double quote inside a quoted field is written twice:
"".
name,phone,note,address
"Uddin, Rahim",01712345678,"He said ""call after 5pm""","House 12, Road 5
Dhanmondi, Dhaka"
Karim,01812345678,,Sylhet
That file has two records, not three: the second line break is inside a quoted address. This is why you must never parse CSV with split(','):
'"Uddin, Rahim",01712345678'.split(',')
// [ '"Uddin', ' Rahim"', '01712345678' ] ✗ three fields instead of two
Use a real parser: Python’s built-in csv module, a library such as Papa Parse in JavaScript, or a converter that follows the quoting rules.
Not every “CSV” uses commas
In countries where the decimal separator is a comma (much of Europe, for example), Excel saves “CSV” with semicolons, because 1250,50 would otherwise be split in two. You will also meet tab-separated files (TSV) and pipes (|). Check the first line before converting, or use a tool that detects the delimiter.
Headers become keys
When converting to JSON, each header becomes a key and each row becomes an object. Clean headers make clean JSON: Customer Name works as a key but is awkward to use in code; customer_name is easier. Watch for empty and duplicate header cells — two columns named phone cannot both be keys of one object, so a converter has to rename one or lose data.
The type-guessing trap
CSV has no types; every cell is text. A converter that “helpfully” turns numeric-looking text into numbers can destroy data:
| Cell text | Naive conversion | What went wrong |
|---|---|---|
01712345678 | 1712345678 | Mobile number lost its leading 0 |
1250.50 | 1250.5 | Formatting of an amount changed |
0012 (product code) | 12 | Different code |
১২৩ (Bangla digits) | NaN | Number() only understands 0–9 |
Rule of thumb: identifiers are text, even when they look like numbers — phone numbers, NID and account numbers, postcodes, invoice numbers. Only convert columns you will actually calculate with. Excel has the same problem when it opens a CSV: it strips leading zeros and keeps only 15 significant digits, so long ID numbers can be permanently changed if you save the file again.
How JSON Bondhu handles it: with “Detect numbers, true/false, null” on, a cell only becomes a number if it converts back to exactly the same text. 980 becomes 980, but 01712345678 and 1250.50 stay strings.
Nested data: dot keys and flattening
JSON can nest; CSV is flat. The usual bridge is dot notation in the headers:
id,customer.name,customer.city
1,রহিম,Dhaka
→ [ { "id": 1, "customer": { "name": "রহিম", "city": "Dhaka" } } ]
Going the other way, JSON → CSV flattens nested objects into dot keys. Arrays have no natural place in a table, so they are usually written as JSON text inside one cell (note the doubled quotes):
[{ "id": 1, "customer": { "name": "রহিম", "city": "Dhaka" }, "tags": ["new", "cod"] }]
→ id,customer.name,customer.city,tags
1,রহিম,Dhaka,"[""new"",""cod""]"
If different objects have different keys, the CSV gets the union of all columns and missing values become empty cells.
Why Excel breaks Bangla text
Bangla characters take three bytes each in UTF-8. When you double-click a CSV, Excel on Windows usually has no way to know the file is UTF-8, so it reads the bytes with the system’s legacy code page (typically Windows-1252). Each Bangla letter becomes three Latin characters, and রহিম turns into রহিম. The file itself is fine — only the reading is wrong. There are three reliable fixes.
Fix 1: save the CSV with a UTF-8 BOM
A byte-order mark is three bytes (EF BB BF) at the very start of the file. Excel sees them and reads the file as UTF-8. When you generate CSV for Excel users, add it:
// Node.js
import { writeFileSync } from 'node:fs';
const csv = 'name,phone,city\r\nরহিম,01712345678,ঢাকা\r\n';
writeFileSync('customers.csv', '' + csv, 'utf8');
# Python: "utf-8-sig" writes the BOM (and strips it when reading)
import csv
with open("customers.csv", "w", encoding="utf-8-sig", newline="") as f:
csv.writer(f).writerows([["name", "phone"], ["রহিম", "01712345678"]])
# pandas
df.to_csv("customers.csv", index=False, encoding="utf-8-sig")
Fix 2: import instead of double-clicking
For a file you received without a BOM: open a blank workbook, go to Data → From Text/CSV, pick the file, set File Origin to 65001: Unicode (UTF-8) and check that the delimiter is right. The preview should show proper Bangla; then click Load. You can also set the phone column to Text here so the leading zeros survive.
Fix 3: save from Excel as “CSV UTF-8”
When you save a sheet containing Bangla, choose CSV UTF-8 (Comma delimited), not plain “CSV (Comma delimited)”. The plain option uses the legacy code page and Bangla characters come out as question marks.
Google Sheets has none of these problems: File → Import reads UTF-8 correctly, and its CSV download is UTF-8.
The BOM cuts both ways. Programs that don’t expect it may read the first header as "name" instead of "name", so row.name is undefined. When reading CSV in code, strip it: encoding="utf-8-sig" in Python, or text.replace(/^/, '') in JavaScript.
A complete CSV → JSON script in Python
import csv, json
with open("orders.csv", encoding="utf-8-sig", newline="") as f:
rows = list(csv.DictReader(f)) # every value stays a string, so 017… is safe
with open("orders.json", "w", encoding="utf-8") as f:
json.dump(rows, f, ensure_ascii=False, indent=2)
newline="" lets the csv module handle line breaks inside quoted fields, and ensure_ascii=False keeps Bangla readable in the output instead of র escapes.
One security note: CSV injection
If a CSV you export contains user-supplied text starting with =, +, - or @, Excel or Google Sheets may treat it as a formula when the file is opened. A common defence is to prefix such cells with an apostrophe so they display as plain text — only do this for files meant for spreadsheets, since it changes the value.
FAQ
I added a BOM but Excel still shows everything in one column. Why?
That is a delimiter problem, not an encoding one. Your Excel probably expects semicolons because of regional settings. Use Data → From Text/CSV and choose the delimiter manually, or export with semicolons for those users.
How do I keep leading zeros when opening a CSV in Excel?
Import it with Data → From Text/CSV and set that column’s type to Text before loading. Double-clicking the file lets Excel guess, and it will turn 01712345678 into a number.
Should empty cells become null or an empty string?
It depends on the receiving system. null usually means “unknown”, "" means “known to be empty”. Pick one rule per column and document it; for numeric columns null is almost always right.