CSV to JSON with type inference — postcodes stay strings
Every cell in a CSV is just text. This decides what each one really is — and keeps the things that should stay text, like a leading-zero postcode, as text.
Writing the inference yourself
Type inference is the part people get wrong, so it is worth seeing it spelled out. The rule is simple: try the most specific type first, and fall back to a string. The important detail is the last one - anything that looks like a number but is not meant to be one has to stay text.
import csv
import json
def infer(value):
"""Guess the type of one CSV cell."""
s = value.strip()
if s == "":
return None # an empty cell is null
if s.lower() in ("true", "false"):
return s.lower() == "true" # a real boolean
if s.lower() == "null":
return None
# Only a clean integer or decimal becomes a number.
try:
return int(s)
except ValueError:
pass
try:
return float(s)
except ValueError:
pass
# Everything else - including "00123" - stays a string.
return s
with open("data.csv", newline="", encoding="utf-8") as f:
rows = [{k: infer(v) for k, v in row.items()} for row in csv.DictReader(f)]
print(json.dumps(rows, indent=2))
Empty becomes null, true/false become booleans, plain numbers become numbers, everything else stays text
Note that int("00123") succeeds and quietly returns 123. That is why the function tries int() but the fallback keeps the original string - a leading zero is a strong signal that the value is an identifier, not a quantity. If you would rather have every value as a string, skip infer altogether and pass the raw cell through.
The converter above has the same switch: leave type inference on for a clean JSON array, or turn it off to keep the source text exactly as it was.
A worked example
Six cells, six different outcomes - including the one that trips people up.
data.csv
id,zip,price,active,notes
1,01234,42.50,true,"Ships, fast"
2,10099,17,true,""
data.json
[
{
"id": 1,
"zip": "01234",
"price": 42.5,
"active": true,
"notes": "Ships, fast"
},
{
"id": 2,
"zip": 10099,
"price": 17,
"active": true,
"notes": null
}
]
Look at zip: the first value keeps its leading zero as text, the second is a plain number. Both came out of the same column, and both are correct - the inference follows the value, not the column. If you want the whole column to be a string, turn type inference off.
Convert the file in the browser
Same file, same rules, and you can watch the types change.
-
Paste the CSV
Or drop a .csv file onto the box. The delimiter is detected automatically - comma, tab, semicolon or pipe.
-
Check the header row
The first row is used as the keys. If your file has no header, switch to the "no header" mode in the settings and the columns get numbered.
-
Decide about types
Type inference is on by default. Turn it off in the settings if every value should stay a string.
-
Download the JSON
Get the array as pretty-printed JSON, ready to paste into a request body.
Getting the types right
A JSON array is only as useful as the types inside it. These are the decisions that matter.
Leading zeros survive
A value like 01234 is kept as a string, because a code that starts with zero is an identifier, not a quantity.
Out-of-range numbers stay text
An integer too large for a 64-bit number is left as a string instead of being rounded in the display.
Real booleans and nulls
true and false become JSON booleans, and an empty cell becomes null rather than an empty string.
Turn it off when you want
One switch in the settings keeps every value exactly as the CSV held it, which is what you want for opaque IDs.
The full CSV to JSON guide
Header handling, quoting, delimiters and the type rules - the whole conversion explained.
Related tools: CSV to JSON converter · JSON to CSV converter · JSON to SQL · JSON to Excel
Frequently asked questions
The rules that decide what each cell becomes.
What types does it detect?
Why did my leading zeros disappear?
00123 inside a larger value can still look numeric. Turning type inference off in the settings keeps every value exactly as the CSV had it.How are empty cells handled?
How do I keep everything as a string?
What about a column that mixes numbers and text?
Convert your CSV with the right types
Free, instant, and private — your CSV never leaves this tab.