JSONPath for API Debugging: Query Nested JSON Without Writing Loops
Use JSONPath to extract fields, filter arrays, and inspect nested API payloads — with practical expressions and a browser tester workflow.
How to convert between CSV and JSON safely — quoting, nested objects, type coercion, headers, and a workflow that catches breakage before it hits production.
By codefunc
CSV is how spreadsheets talk. JSON is how APIs talk. Moving data between them looks trivial until a nested address becomes a stringified blob, a leading zero disappears from a ZIP code, or a field containing a comma splits into two columns. Most interchange bugs are silent: the file still "opens," but values are wrong. This guide covers the rules that keep CSV ↔ JSON conversions honest.
Bottom line: Treat CSV as a flat table of strings; treat JSON as typed trees. Flatten before export, validate after import. Use JSON to CSV and CSV to JSON as checkpoints, and format intermediate JSON with a JSON Formatter so diffs stay readable.
| Concern | CSV | JSON |
|---|---|---|
| Shape | Rectangular rows × columns | Nested objects and arrays |
| Types | Everything is text until a consumer guesses | string, number, bool, null, array, object |
| Nesting | Awkward (multiple columns or JSON-in-cell) | Natural |
| Spreadsheet edit | Excellent | Poor |
| API / config | Rare as primary | Default |
| Comments | No | No (strict JSON) |
Rule of thumb: export to CSV for humans in Excel/Sheets; keep JSON for machines and nesting.
JSON can look like this:
[
{
"id": "ord_1042",
"customer": { "name": "Ada Lovelace", "email": "ada@example.com" },
"items": [
{ "sku": "BOOK-01", "qty": 2 },
{ "sku": "PEN-03", "qty": 1 }
],
"total": 42.5
}
]
CSV has no native object or array. Common strategies:
| Strategy | Result | Tradeoff |
|---|---|---|
| Flatten with dotted keys | customer.name, customer.email |
Loses arrays unless you explode rows |
| One row per line item | Duplicate order headers | Good for analytics; verbose |
| JSON string in a cell | items column holds [{...}] |
Hard to edit in Sheets; quoting hell |
| Drop nested data | Only top-level scalars | Data loss — be explicit |
Before you run JSON to CSV, decide which strategy you want. Do not discover it from a surprise column named [object Object].
[
{
"id": "ord_1042",
"customer_name": "Ada Lovelace",
"customer_email": "ada@example.com",
"sku": "BOOK-01",
"qty": 2,
"total": 42.5
},
{
"id": "ord_1042",
"customer_name": "Ada Lovelace",
"customer_email": "ada@example.com",
"sku": "PEN-03",
"qty": 1,
"total": 42.5
}
]
One order, two rows. Totals repeat — that is fine for export; re-aggregate when you import if needed.
RFC 4180-style CSV expects:
" escaped as ""id,note
1,"He said ""ship it"", then left"
2,"Line one
Line two"
| Pitfall | What happens | Fix |
|---|---|---|
| Unquoted comma in a field | Extra columns | Quote the field |
| Single quotes only | Some parsers treat ' as data, not a wrapper |
Use " |
| Semicolon locales | Excel in some regions exports ; |
Detect delimiter; do not assume , |
| Trailing comma | Empty extra column | Strip or accept empty header |
| UTF-8 BOM | First header becomes \ufeffid |
Strip BOM on import |
When something looks "almost right," paste the CSV into CSV to JSON and inspect the parsed keys. Wrong column counts show up immediately.
CSV without a header row is guesswork. With a header row, the header is your schema for that export.
sku,qty,price
BOOK-01,2,19.99
PEN-03,1,2.50
Becomes:
[
{ "sku": "BOOK-01", "qty": "2", "price": "19.99" },
{ "sku": "PEN-03", "qty": "1", "price": "2.50" }
]
Note: many converters leave qty and price as strings. That is safer than guessing. Coerce types in application code or a second pass when you know the column types.
| Rule | Why |
|---|---|
| Stable, unique names | Duplicate headers collide or overwrite |
| No spaces if avoidable | Prefer customer_email over Customer Email |
| Same column order across exports | Makes diffs and appends sane |
| Document units in the name | price_usd, weight_kg |
Format the resulting array with a JSON Formatter before you commit fixtures or send them to another team.
Spreadsheets and "smart" importers love to help:
| Original | After Excel / loose parse | Reality |
|---|---|---|
00123 (ZIP / SKU) |
123 |
Leading zeros gone |
10/11/12 |
Locale-dependent date | Ambiguous |
TRUE / FALSE |
boolean or string | Inconsistent |
| empty cell | "", null, or missing key |
Schema drift |
1e3 |
number 1000 or string |
Precision / intent |
Prefer stringly CSV for identifiers (IDs, ZIP codes, phone numbers, account numbers). Convert to numbers only for measured quantities you will compute on.
Workflow:
Number(row.qty), keep row.sku as string.JSON distinguishes null, missing keys, and "". CSV usually has only "empty cell."
| CSV cell | Reasonable JSON mapping |
|---|---|
| empty | "" or omit key (pick one policy) |
literal null |
string "null" unless you special-case |
0 |
string "0" until coerced |
Document the policy. Mixed null and "" for "no email" will break uniqueness checks and email validators.
Browser paste tools are ideal for samples, fixtures, and debugging. For multi‑million‑row dumps:
csvkit, jq, Python csv + json) or your warehouse loadersClient-side converters still shine when the payload must not leave your machine — paste a redacted sample, confirm shape, then run the batch job locally.
Sometimes a column must hold a list (tags, error details). Store compact JSON in one cell, quote it properly, and parse that column separately:
id,tags
1,"[""urgent"",""billing""]"
Document that the column is JSON. Do not expect casual spreadsheet users to edit it safely.
CSV and JSON interoperate only when you treat CSV as a flat string table and JSON as a typed tree. Flatten with an explicit row grain, quote fields that need it, and never let spreadsheet auto-types touch identifiers. Convert with JSON to CSV and CSV to JSON, then verify structure in a JSON Formatter. Round-trip a small fixture before you trust a full export — silent column splits and lost leading zeros are cheaper to catch on ten rows than on ten thousand.
Use JSONPath to extract fields, filter arrays, and inspect nested API payloads — with practical expressions and a browser tester workflow.
When to use JSON, YAML, or TOML for app config — conversion pitfalls, trailing commas, multiline strings, and a safe workflow for editing and validating files.
How to compare JSON structurally — key order, arrays, nested objects — and when to use JSON diff versus line-based text diff in config and API review workflows.
Find a developer tool