How to Pretty-Print JSON from Logs and APIs

Strip the log prefix, validate the remaining string, then format. Do not pretty-print a line that still has a timestamp, a level, or a PHP notice attached.

Last updated: October 2, 2026

Quick answer

To pretty-print JSON from logs, copy only the JSON value. Delete the timestamp, log level, process id, and any PHP or Magento notice that printed before the first { or [. Paste what remains into the JSON Validator. If it is valid, paste the same string into the JSON Formatter and choose Format. If it is invalid, fix the first token. Do not format first.

Minified API responses follow the same order. The body is often already legal JSON. It is unreadable because it has no whitespace, not because it is broken. Validate, then format. That is how you pretty-print JSON from logs and from APIs without guessing.

What usually sits in front of the payload

A log line is a record, not a JSON document. Typical prefixes:

  • ISO time and a level: 2026-09-24T09:12:01Z ERROR
  • A channel or request id
  • A PHP notice or Magento exception header printed to the same stream
  • A BOM or a UTF-8 ef bb bf if the file was saved from a Windows editor

The validator fails at position 0 when any of that is still attached. People then blame the formatter. The formatter never saw JSON. It saw ERROR {"user": or Notice: … {"order":.

API clients add a different kind of junk: a wrapping string that was escaped twice ("{\"id\":1}"), or HTML from an error page when the endpoint returned 500 with a body that is not JSON at all. Treat those as different jobs. This guide is for a line that is supposed to contain one JSON value.

Isolate the JSON value

  1. Find the first { or [. If the value is a JSON string, number, true, false, or null, there is no brace — copy the token the logger stored, not the whole line.
  2. Find the matching close. If the logger truncated the line, you do not have JSON. Ask for the full field or raise the log limit. Do not invent the missing tail.
  3. If the field is quoted as a string inside another JSON object, decode one layer. A value that starts with "{" and contains \" is a string, not an object. Unescape once, then validate the inner text.
  4. Strip a leading BOM. If the first character looks like a brace but validate still fails at 0, copy the hex or paste into an editor that shows invisible characters.

Do not run a regex that “grabs everything between the first brace and the last.” Nested objects and braces inside strings will lie to you. Prefer copying the field the logger named (body=, payload=, context=) when the format is structured.

Validate, then format

Paste the isolated string into the validator. Empty input is not JSON. A trailing comma, a single-quoted key, or a comment means you isolated a JavaScript dump, not JSON. Convert that dump with the object-literal guide, then come back here.

When validate reports a valid object or array, format it. Indentation is for you, not for the server. If you will send the body again, minify after you finish reading — a pretty-printed body is a different artifact than the log field you started with. The order matches format, validate and minify JSON.

YallaSolve does not upload the payload for these tools. The string still lives in the tab. Do not paste live tokens into a shared machine and walk away.

Worked examples

Log line

2026-09-24T09:12:01Z ERROR checkout id=881 body={"user":{"id":12,"name":"Ada"},"total":19.5}

Copy from the first { through the last }. Validate: valid object. Format: you can see user and total. If total is the bug, you found it because you could read the tree, not because you stared at one line.

Minified API body

{"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1}],"ok":true}

No prefix. Validate: valid. Format: the two line items are obvious. You do not minify this unless you are putting it back on the wire.

Escaped twice

"{\"ok\":true,\"n\":2}"

This is a JSON string whose content is JSON. Paste it as-is and validate: you get a string, not an object. Remove the outer quotes and unescape \" to ", then validate again. Now format. If you format the outer string, you only pretty-print a quoted blob and still cannot see ok.

When the line is escaped twice

WordPress, Magento, and some PHP loggers json_encode a value and then interpolate it into another JSON envelope or a quoted log field. You will see backslashes in front of every quote. Decode one layer at a time. After each layer, validate. Stop when the validator reports an object or array you recognize, or when a layer is plain text.

If validate never succeeds, the field is not JSON. It may be print_r, a stack trace, or HTML. Do not keep hitting Format. Open the parse-error solution for the token you have, or rebuild the payload from the application.

Pretty-print is a reading step. It does not fix production. Once you can see the structure, decide whether the bug is a bad value, a missing key, or a wrap you should not have logged. Then delete the formatted copy if it contains customer data.

Need a tool for this ?

Open the free tools — no signup.

FAQs

newsletter signup

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Innovative Solutions For Modern Needs
Copyright © 2026 Yallasolve. all rights reserved.