Documents & OCRPractical guides

CSV and JSON conversion: reconcile three records, blanks and line breaks

Reconcile CSV/JSON conversions by records and field values rather than text-line counts or appearance. This complete fictional CSV and reference JSON include commas, a line break and quotes. Follow both directions and a repair case while preserving 0007, a blank amount and string 0.

Lines of HTML source code on a computer screen
Illustrative photo: Florian Olivo · Unsplash License

What changed: Added a complete three-record round trip, reference JSON for nine field values, a quoting failure and record-level reconciliation.

Key Takeaways

The CSV occupies five physical lines but contains three data records. Forward conversion must return three objects with three string fields each; Amount is "12.50", "" and "0". Parse the reverse CSV and compare all nine values. Changed line endings or quoting style alone do not establish changed data.

In this article

Complete input: comma, embedded newline and double quotes together

Download source.csv or copy the entire block. ID,Note,Amount is the only header. The first note contains a comma, the second a real newline and the third double quotes. These are fictional records; amounts remain text in this converter.

  1. Open CSV/JSON conversion, paste the whole input and run forward conversion. Do not split records on newlines or replace the note’s comma first.
  2. Compare the three objects with the complete reference JSON. Check full IDs, newline/quote content and the three Amount strings, rather than accepting a success message alone.
  3. Download or copy the JSON and retain the CSV. If the source uses semicolons or another encoding, first inspect it through the CSV import/type workflow; this text converter expects comma-separated CSV.

Complete source.csv input

ID,Note,Amount
0007,"First, then check",12.50
0008,"Line one
Line two",
0009,"He said ""OK""",0
Five physical lines, three data records; no extra empty record. The quoted newline belongs to one Note.

Complete reference JSON

[
  {
    "ID": "0007",
    "Note": "First, then check",
    "Amount": "12.50"
  },
  {
    "ID": "0008",
    "Note": "Line one\nLine two",
    "Amount": ""
  },
  {
    "ID": "0009",
    "Note": "He said \"OK\"",
    "Amount": "0"
  }
]
The JSON escape \n represents one real newline inside Note. Empty string and string 0 remain distinct.

RFC 4180’s CSV description explains quoting for commas, newlines and double quotes. Byte identity, formatting and type preservation are separate acceptance questions.

Reverse conversion: compare parsed values, not identical bytes

Put the complete forward result back into the input and run reverse conversion. The tool requires a nonempty flat array of objects. Expand nested objects/arrays according to their meaning instead of deleting fields merely to pass validation.

  1. Retain this JSON, generate/download reverse CSV, then run that CSV forward again to obtain a second JSON copy.
  2. Match the records by ID:0007 keeps First, then check;0008 keeps one newline and an empty Amount;0009 keeps "OK" and string 0.
  3. The field set must remain ID,Note,Amount with three fields in every object. Key order or CSV line endings may change; this exercise accepts equality of parsed records and values.
Critical values to retain in each record
IDNote checkAmount check
0007Comma stays within NoteString 12.50
0008One real newline between Line one and Line twoEmpty string
0009He said "OK" retains its quotesString 0

CSV does not carry every JSON type distinction. An absent key, null and empty string can collapse into an ordinary blank cell. Use the explicit state-column method below when these states matter, rather than guessing from a blank.

Why five physical lines contain only three data records

There is one header line, one first-record line, two lines for the second record and one final-record line: five physical lines. Quoting determines record boundaries, so a newline split is not a record count.

Record ledger for this exercise
StructureCountCheck
Header1ID,Note,Amount column names
Data records3Three JSON objects and three IDs
Field values9Three records×three columns, including an empty string
Embedded note newline1Part of 0008, not a fourth record

Add a state column when missing and explicitly empty differ

Suppose illustrative JSON record A has phone: null, B has no phone property, and C has phone: "". All three export to blank phone cells in CSV. Converting back cannot recover which original state each blank represented. This is a loss of structure, not something repeated conversion can repair.

If the distinction matters, prepare a flat schema with id, phone and phone_state before conversion. Give A the state explicit_null, B missing and C empty_string. This is a convention you define and explain to the recipient, not a field generated automatically by the converter. The recipient can reconstruct meaning from the state. When the distinction does not matter, accepting blanks avoids unnecessary extra fields.

Illustrative mapping: double quotes represent an empty phone field
Sourcephonephone_state
{"id":"A","phone":null}""explicit_null
{"id":"B"}""missing
{"id":"C","phone":""}""empty_string

Repair the structural cause, then review the complete records

For a “4 fields; header has 3” error, locate that logical record first. If the source is 0007,Ada,red, blue and red, blue is one note, enclose that note instead of deleting its comma and changing the wording. For an id,id,note header, establish distinct meanings and names for the two identifier columns. Merely deleting one header leaves later values without their intended positions.

An unclosed quote requires checking where the source field actually ends. Appending a quote to the end of a file can make several orders appear to be one large note. After repair, compare the expected ID set, record count and first and last notes to establish that structure was restored without rewriting content.

Repair structure, then accept the complete round trip

The pack’s bad-width.csv puts 0008,unquoted, comma,0 under three headers: four fields, so the current converter must reject it. If the intended note is unquoted, comma, repair the row as 0008,"unquoted, comma",0. Removing the comma may restore width while changing the original note.

Three-record handoff acceptance

  • Preserve all three IDs character for character;0007 stays 0007, with no added or missing field.
  • Keep the comma, one embedded newline and "OK" in their own notes.
  • Amount remains string 12.50, empty string and string 0 without an invented numeric/null conversion.
  • Reparse the returned CSV and compare all nine values with the answer; keep the original and correction record.

A calculation-ready workbook needs separate type decisions. Continue with JSON-to-Excel ID and calculation-column checks rather than assuming structural conversion validates monetary or identifier types. For notices, confirm variables and source columns using the Word template workflow.

Common questions

Does it support nested JSON?

JSON to CSV here rejects object or array field values. Expand the required fields into named columns first.

Are null and missing fields kept distinct?

No. Both become empty CSV cells in this conversion. Add a status column when the difference matters.

Can I upload Excel directly?

This entry accepts pasted text. Export Excel as CSV in a spreadsheet tool, then review and convert the text.

Further reading & sources

Put the guide into practice

Open the relevant tool, preview the result and check your exported file.

Newer articles on this topic

Continue with a newer article that refers to this guide or explainer.