October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Convert JSON to Compact JSON or CSV Without Losing Data

Compact JSON by removing formatting whitespace with jq. For CSV, define the row model and how nested, missing, null, and empty values will be represented.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To make JSON compact, remove formatting whitespace while keeping the JSON structure: jq -c . input.json. To convert JSON to CSV, first decide how nested objects, arrays, missing keys, and null values should map to rows and columns. Compacting is a formatting change; CSV conversion is a data-shaping step, so it needs an explicit mapping.

Compact JSON and CSV solve different problems

Compact JSON remains JSON. It removes layout whitespace such as indentation and line breaks, but retains the parsed values and their structure. The JSON standard allows insignificant whitespace around structural characters, so removing that whitespace need not alter the JSON value. RFC 8259

As an Amazon Associate I earn from qualifying purchases.

CSV, by contrast, arranges fields into records. JSON supports primitive values—strings, numbers, booleans, and null—as well as objects and arrays. Objects can contain nested values; arrays are ordered and can contain mixed types. A CSV export therefore needs rules for choosing records, columns, and representations for values that do not fit naturally into a flat table. RFC 8259

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Minify JSON while retaining its parsed value

With jq installed, write compact output to a new file:

#1 Best Overall
Pocket Ref
  • Author: Thomas Glover
  • 864 pages
  • 3.2" x 5.4", softbound
  • (Also available in Desk Size item 2072)
jq -c . input.json > compact.json

The -c option, also called --compact-output, puts each JSON output value on one line. For an ordinary JSON input, this changes serialization and layout rather than converting the data into a table. Validate the output with a JSON parser. When checking whether content was retained, compare parsed values rather than text: whitespace and object member order may differ without changing the represented value.

Do not add jq’s -r option when the goal is JSON output. Raw mode writes string results without JSON string quoting, which can make the output unsuitable as a JSON document.

Parsing and reserializing is not byte-for-byte preservation. It may change whitespace, escape spellings, serialization choices, or member order. If exact source bytes matter, keep the original file. Duplicate object names are an additional hazard: RFC 8259 says names should be unique, and parser behavior for duplicates can vary—some implementations keep the last value, reject the object, or expose duplicates. If every textual occurrence matters, inspect for duplicates before ordinary parse-and-convert processing. RFC 8259

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a CSV mapping before converting

  1. Define what counts as a record. Identify the array or other collection of records, or decide how a single object or top-level scalar should become rows. A JSON document is not necessarily an array of flat objects.
  2. Choose the columns and headers. For records with different keys, decide whether columns come from a fixed schema, the union of keys, or a selected subset. Keep the decision consistent so each value lands under the intended header.
  3. Set a policy for nested values. Choose whether to put an object or array into one cell as JSON text, flatten object paths into columns, or expand array elements into rows. These produce different table shapes; flattening and expansion are not neutral formatting changes.
  4. Specify how to represent missing and empty values. Decide how absent keys, null, empty strings, empty arrays, and empty objects appear. If the CSV representation could make two source states look the same, define sentinels or keep a schema or sidecar that records the distinction.
  5. Use a CSV serializer and check the result. Do not create rows by simply joining values with commas. Verify headers, row alignment, and how the receiving application imports the file.

Pick a nested-value strategy

Strategy What it does Trade-off
JSON text in a cell Stores an object or array as serialized JSON in one CSV field. Retains the nested value’s structure as text, but the cell is not native nested data and may be less convenient to analyze.
Flatten object paths Turns nested object properties into columns, such as a path representing a child property. Can make fields easier to inspect in a table, but changes the shape and requires consistent path and collision rules to reconstruct reliably.
Expand arrays into rows Creates one or more rows from array elements. Can help with row-based analysis, but may repeat parent values and lose element grouping unless identifiers and expansion rules are retained.

For the least destructive export of nested values, keeping each object or array as JSON text in a cell is often the clearest choice. It does not make CSV a fully reversible substitute for JSON: the value is text in a field, and the CSV still needs a defined schema and escaping rules.

Escape CSV fields and test edge cases

Under the common CSV convention described by RFC 4180, fields containing commas, double quotes, or line breaks should be enclosed in double quotes; a double quote inside a quoted field is doubled. Use a serializer that applies the rules rather than building CSV by hand. RFC 4180

Before relying on a conversion, check representative input containing commas, quotes, CR/LF line breaks, Unicode, nulls, empty values, absent keys, nested objects, nested arrays, and mixed-type array elements. These are validation cases to run against the specific converter and receiving application; behavior can depend on the tool and its settings.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What “without losing data” can mean

For compact JSON, it can mean preserving the parsed JSON value while changing its serialization. It does not guarantee the original bytes, whitespace, escape spellings, or member order.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For CSV, it can mean retaining selected values under a documented mapping. CSV does not inherently preserve JSON’s nested types or distinguish an absent key from null or an empty string in every representation. Reconstructing the original reliably requires a reversible encoding convention and its schema. If exact preservation of arbitrary JSON structure is required, keep JSON as the canonical file and treat CSV as a defined export view.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.