Many tools want JSON rather than CSV. Convert a CSV table into one JSON object per line, with every value given its proper type.
What to build
A program that converts CSV rows into typed JSON objects.
Requirements
- Read the lines with the csv module. Convert each value: an empty field becomes
null; a whole number (optional minus sign, then digits) becomes an integer; anything else thatfloat()accepts becomes a decimal;trueandfalse(lower case) become booleans; anything else stays a string. - Print each data row as
json.dumpsof a dictionary with the header's keys in order, thenrows converted: N.
Input
Line 1: n, the number of CSV lines including the header. Then the n lines.
Output
One JSON object per data row, then the count.
Constraints
1 ≤ n ≤ 1000; values never contain line breaks.
Example
Input:
3
id,name,score,passed
1,Asha,88.5,true
2,"Lee, Jr.",,false
Output:
{"id": 1, "name": "Asha", "score": 88.5, "passed": true}
{"id": 2, "name": "Lee, Jr.", "score": null, "passed": false}
rows converted: 2
Why it works
Converting each field in a fixed order of checks gives every value its natural type once, at the boundary, so the JSON consumers get numbers, booleans and nulls instead of strings they would have to guess about.
Examples
3 id,name,score,passed 1,Asha,88.5,true 2,"Lee, Jr.",,false
{"id": 1, "name": "Asha", "score": 88.5, "passed": true}
{"id": 2, "name": "Lee, Jr.", "score": null, "passed": false}
rows converted: 2
2 x 007
{"x": 7}
rows converted: 1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Write convert(text) that tries, in order: empty, whole number, decimal, true/false, and otherwise keeps the text.
Hint 2
Build each row as a dictionary in header order; json.dumps keeps that order.
Solution
Show one correct solution (try it yourself first)
import csv
import json
import re
n = int(input())
rows = list(csv.reader([input() for _ in range(n)]))
header, data = rows[0], rows[1:]
def convert(text):
if text == "":
return None
if re.fullmatch(r"-?\d+", text):
return int(text)
try:
return float(text)
except ValueError:
pass
if text in ("true", "false"):
return text == "true"
return text
for row in data:
print(json.dumps({key: convert(value) for key, value in zip(header, row)}))
print("rows converted:", len(data))