Parse a JSON response
How parsing, pretty-printing and re-serializing a JSON response change your data, when meaning is preserved, and what breaks when JSON is copied or shared.
Parsing a JSON response means turning the text an API sends back into data your program can work with, such as objects, arrays, strings, numbers, booleans and null. Developer text tools sit around this step. They validate the text, pretty-print it for reading, minify it for transport, or parse it and write it back out. These operations look similar, but they make different kinds of changes, and some of them can quietly alter what the data means. This review explains what each operation changes and when the result can be trusted, copied or shared.
No tool-specific evidence, measurements or test results were supplied for this review. The guidance below rests on the JSON specification (RFC 8259) and documented behavior of common language parsers. Where behavior depends on a particular tool or destination, it is flagged rather than assumed.
Is parsing or reformatting suitable for your use?
For reading, debugging and consuming API data, parsing is the right approach. A real parser is the only reliable way to confirm that a response is valid JSON and to get at its values. Pretty-printing is also safe for inspection, because it only adds whitespace between tokens. That whitespace has no meaning in JSON.
The main tradeoff appears when a tool parses the response and then writes it back out as new text. The output is valid JSON, but it is not guaranteed to match the original byte for byte, and in some edge cases it does not carry the same values. Number precision, duplicate keys, key order and character escaping are where these differences show up.
Keep the original raw text whenever exactness matters. Examples include verifying a signature or checksum over the body, comparing responses for changes, storing evidence of what an API returned, or handling numeric identifiers longer than about 15 to 16 significant digits. In those cases, treat the parsed or pretty-printed version as a view of the data and keep the raw response as the record.
What each transformation actually changes
In this context, 'formatting' means arranging whitespace and line breaks. It does not mean changing fonts or substituting styled Unicode characters. Reversal and mirror effects do not apply to JSON either. Visual styling of that kind would break the data: a key written in lookalike Unicode letters is a different string and will not match the key your code expects.
| Operation | What changes in the stored text | Is meaning preserved? |
| Validate | Nothing; the text is only checked | Yes, the text is untouched |
| Pretty-print (whitespace only) | Spaces, tabs and line breaks between tokens | Yes, if the tool only re-indents and does not re-serialize |
| Minify | Whitespace between tokens is removed | Yes, under the same condition |
| Parse into a data structure | Text becomes in-memory values | Usually, but number precision and duplicate keys depend on the parser |
| Parse, then re-serialize | The output text is newly generated | Values usually survive, but the exact representation can change |
| Escape or unescape non-ASCII | For example, \u00e9 becomes é or the reverse | Yes, both forms decode to the same string |
| Sort keys | The order of members within objects | Yes under the JSON data model, although the text differs |
The difference between stored text and presentation matters here. A tree viewer, collapsible panel or syntax highlighting changes only how JSON is shown on screen, and the underlying text stays the same. A formatter that outputs new text has changed the characters, even if the meaning is the same. When a tool shows you formatted output, check whether it re-indented the original or parsed and regenerated it. Many tools do not say which approach they use.
Where re-serializing can alter values
- Large integers: JavaScript parses every JSON number as a 64-bit floating-point value, so integers above 9007199254740991 can be rounded silently. Python keeps JSON integers exact.
- Number spelling: 1.0, 1e2 and 100.00 may come back as 1, 100 and 100. The value is equal, but the text is different.
- Duplicate keys: RFC 8259 says object names should be unique and that behavior with duplicates is unpredictable. JavaScript's JSON.parse and Python's json module keep the last value, so the earlier ones disappear when the data is re-serialized.
- Non-standard values: Python's json module accepts NaN and Infinity by default, but these are not valid JSON and other parsers will reject them.
- Escaping: Python's json.dumps escapes non-ASCII characters as \uXXXX sequences by default, while JavaScript's JSON.stringify leaves most of them as literal characters. Both outputs are valid, but they look different.
JSON.parse('{"id": 9007199254740993}').id
// 9007199254740992 (rounded; the original value is lost)
JSON.stringify(JSON.parse('{"price": 1.0}'))
// '{"price":1}'
If an API returns long numeric IDs, prefer one that sends them as strings. If that is not possible, use a parser option or library that preserves large integers. Do not rely on a formatter that round-trips the data through standard number handling.
Parsing a response safely in code
In code, a JSON response can fail in two separate ways. The HTTP request can return an error status, or the body can be empty or not JSON at all, such as an HTML error page from a proxy. Check for both before using the data.
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
let data;
try {
data = await response.json();
} catch (err) {
// Body was empty or not valid JSON
throw new Error("Response was not valid JSON");
}
import requests
response = requests.get(url, timeout=10)
response.raise_for_status()
try:
data = response.json()
except ValueError:
# Body was empty or not valid JSON
raise RuntimeError("Response was not valid JSON")
The fetch API does not reject the promise for HTTP error statuses, which is why the response.ok check is needed. Neither response.json() in fetch nor response.json() in requests checks the Content-Type header. Both simply try to parse the body. Python's requests raises a JSON decode error that subclasses ValueError, so catching ValueError covers both older and newer versions.
Will the result stay usable after copying or sharing?
Pretty-printed and minified JSON are both plain text. Either one can be copied into an editor, terminal or code file and parsed again without loss. Problems usually come from the place where the text is pasted, not from the formatting.
- Smart quotes: word processors and some chat or note apps may replace straight double quotes with curly ones. JSON only accepts the straight quote character, so the result will no longer parse.
- Invisible characters: JSON allows only space, tab, line feed and carriage return as whitespace between tokens. A non-breaking space or zero-width character introduced by copying from a web page or document makes the text invalid, even though it looks the same.
- Byte order mark: RFC 8259 says JSON text must not begin with a byte order mark. Some parsers ignore it, while others, including Python's json.loads on a string, reject it.
- Encoding: JSON exchanged between systems is expected to be UTF-8. Text re-saved in another encoding can corrupt non-ASCII characters.
- Truncation: long single-line minified responses may be cut off by some log viewers or message fields, which leaves invalid JSON.
Whether a specific chat app, document editor or ticketing system alters pasted JSON depends on that product and its settings. No such destination was tested for this review. The dependable approach is to share JSON inside a code block or as an attached .json file, then run the received copy through a validator before relying on it.
Accessibility deserves separate consideration. Screen reader output for JSON depends on the screen reader, its punctuation settings and the editor or viewer. No assistive technology was tested here, so no specific behavior is claimed. Deeply nested JSON relies on indentation and bracket matching, and these may not be conveyed clearly when read aloud. Collapsible tree viewers are visual aids that may or may not be operable by keyboard or screen reader. When a response is shared with people rather than programs, a short plain-language summary of the relevant fields is often more usable than the raw structure. Examples include status, key values and any error message.
Search and editing work on the stored text, not on how it looks. Searching for a value written with a \u escape in one copy and as a literal character in another may not find both. Sorted or re-indented output also makes line-based diffs against the original noisy. For editing, change the parsed data in code, or edit the text in an editor that validates JSON, so mistakes are caught before the file is used.
Before pasting a response into an online tool
API responses often contain access tokens, session identifiers or personal data. A web-based formatter's appearance or marketing does not tell you how it handles what you paste, so check its documented privacy and retention terms. For sensitive responses, a local option is safer. Examples include your editor's built-in formatter, a command-line tool, or your language's own parser, such as python -m json.tool or jq where installed.
Bottom line
Parsing and whitespace-only formatting keep the meaning of a JSON response and make it much easier to read and validate. Re-serialized output is fine for everyday inspection. It is not a substitute for the original text when you need exact numbers, duplicate-key detection, signatures or faithful records. For those cases, keep the raw response. When sharing JSON with people, add a plain-text summary of what matters.