DEV Community

help
help

Posted on Fully Autonomous

Inspecting a JSON download: root types, escaped layers, and safe minification

A JSON file can be valid and still be the wrong shape for the next step. A log entry may contain a JSON string whose contents are another document; a download may contain one JSON object per line; a formatter may hide a numeric precision change behind neat indentation.

Disclosure: this guide is published on behalf of QoTool and links to our own tools. It was prepared with AI assistance. The examples below use synthetic data.

1. Identify the container before editing

If you are figuring out how to open json files, start with a copy of the file and inspect the first meaningful character. An opening brace suggests an object, a bracket suggests an array, and a quote may mean the entire document is a string. These are clues, not validation: JSON can also hold a number, boolean or null at its root.

QoTool's file viewer offers an Open file button and a bounded preview. Its displayed input limit is 5 MB. Do not assume a short preview means the original file contains only those rows. Compare the expected record count before using an export.

A file ending in .json is not automatically a single JSON document. If each line contains an independent object, use a JSONL-aware reader rather than adding brackets without checking commas and blank lines.

2. Decode one layer at a time

This JavaScript example constructs a document, then wraps the document text in a JSON string:

const original = { message: 'A "quoted" label', path: String.raw`C:\temp` };
const documentText = JSON.stringify(original);
const wrappedText = JSON.stringify(documentText);

const once = JSON.parse(wrappedText);
console.log(typeof once); // "string"
const twice = JSON.parse(once);
console.log(twice.message); // A "quoted" label
Enter fullscreen mode Exit fullscreen mode

The second parse is justified here because we constructed a known wrapper. In a real log, inspect the value after the first parse. A legitimate string such as a customer's note should remain a string.

To unescape json interactively in QoTool, choose Unescape JSON string and supply the complete quoted string. Do not remove every backslash with a search-and-replace: escaped quotes, newlines and literal backslashes have different meanings.

3. Compact syntax without changing content

For a small JSON document with ordinary numeric values, this produces compact JSON:

const source = '{ "label": "two words", "enabled": true }';
const compact = JSON.stringify(JSON.parse(source));
console.log(compact); // {"label":"two words","enabled":true}
Enter fullscreen mode Exit fullscreen mode

Notice that the space inside "two words" remains. Removing all whitespace with a regular expression would change that value. When you minify json, compare parsed content and types as well as file size.

There is an important numeric limitation to this JavaScript shortcut: an integer larger than Number.MAX_SAFE_INTEGER can lose precision during JSON.parse. If a system's identifiers are strings, preserve them as strings. If its contract requires large JSON numbers, use a precision-preserving parser and test the exact digits through the whole pipeline. Do not silently convert every number to a string; that changes the schema.

4. Keep a small acceptance set

Before saving a transformed file, check these cases:

  • A string containing a double quote, a backslash and a newline.
  • An empty string, an empty array and an empty object.
  • A null value alongside a missing property; they are not equivalent.
  • A numeric-looking identifier such as "0007" that must retain leading zeros.
  • A large integer supplied by the real application's schema.

Record the expected root type and record count with your fixture. Keep the original alongside the result. Formatting, decoding and repairing are separate operations: a repair that makes malformed input parseable can still make the wrong guess about what the producer intended.

The useful success criterion is simple: the next consumer accepts the result, and the fields you care about retain their values and types. A smaller file or a tidy tree view is only part of that check.

Top comments (0)