Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool

Delphi/Pascal JSON Formatting Components

If you are looking for a Delphi/Pascal JSON formatting component, the short answer is that modern tools already include one. In current Delphi, the built-in System.JSON classes can pretty-print a parsed JSON value with Format() and serialize compact JSON with ToJSON. In Free Pascal and Lazarus, the equivalent built-in path is fpjson, where TJSONData exposes FormatJSON() for readable output and AsJSON for compact output.

That matters because many searchers do not actually need a third-party visual component. They need the fastest reliable way to inspect API payloads, prettify logs, or emit readable config files from Pascal code. This page focuses on that practical use case.

Quick Answer

  • Delphi / RAD Studio: Parse JSON into a TJSONValue and call Format(2) or Format(4) for pretty output.
  • Compact Delphi output: Use ToJSON when you want a transport-friendly string without pretty-print whitespace.
  • Free Pascal / Lazarus: Use TJSONData.FormatJSON() for readable output and AsJSON for compact output.
  • Third-party libraries: Usually only needed when formatting is part of a larger requirement such as streaming very large payloads, custom serialization, or framework integration.

Delphi / RAD Studio: Built-In JSON Formatting

Embarcadero's current RTL docs expose JSON pretty-printing directly on TJSONAncestor via Format(Indentation: Integer = 4). That means you normally do not need a writer class just to format JSON for humans.

Current Delphi example using System.JSON

uses
  System.JSON,
  System.SysUtils;

var
  JsonText: string;
  JsonValue: TJSONValue;
begin
  JsonText := '{"name":"Alice","roles":["admin","editor"],"active":true}';
  JsonValue := TJSONObject.ParseJSONValue(JsonText, False, True);
  try
    Writeln('Pretty JSON:');
    Writeln(JsonValue.Format(2));

    Writeln('');
    Writeln('Compact JSON:');
    Writeln(JsonValue.ToJSON);
  finally
    JsonValue.Free;
  end;
end;
  • Use exceptions when validating input: Passing RaiseExc = True helps you fail fast on malformed JSON instead of silently getting nil.
  • Indentation is simple and predictable: Format(2), Format(4), and similar calls control the number of spaces per nesting level.
  • Compact output uses JSON escaping rules: ToJSON is the better fit when the string is leaving your app rather than being read by a person in a debugger.

Free Pascal / Lazarus: fpjson

For Free Pascal projects, the fcl-json package provides the equivalent feature set. The current docs describe TJSONData.FormatJSON(Options, IndentSize) for formatted output and AsJSON for the compact representation.

Current Free Pascal / Lazarus example using fpjson

uses
  fpjson,
  jsonparser,
  SysUtils;

var
  Data: TJSONData;
begin
  Data := GetJSON('{"name":"Alice","roles":["admin","editor"],"active":true}');
  try
    Writeln('Pretty JSON:');
    Writeln(Data.FormatJSON([], 2));

    Writeln('');
    Writeln('Compact JSON:');
    Writeln(Data.AsJSON);
  finally
    Data.Free;
  end;
end;

One useful detail from the Free Pascal docs: FormatJSON is convenient, but it is not optimized for speed. That is fine for tooling, debugging, admin screens, and occasional exports, but it is a poor choice for tight loops or very large payloads on hot paths.

Choosing the Right Approach

  • Use the built-in RTL first in Delphi: If your only goal is pretty-printing or compacting JSON, System.JSON already covers it cleanly.
  • Use fpjson first in Free Pascal: It solves the same problem without adding another dependency to a Lazarus or CLI project.
  • Add third-party JSON libraries only for broader needs: Think object mapping, custom serializers, framework conventions, or specialized performance requirements.
  • Use an external formatter tool for inspection workflows: When you are comparing API responses, checking malformed payloads, or formatting ad-hoc JSON from logs, a dedicated formatter is usually faster than writing a helper app.

Common Pitfalls

  • Using outdated conceptual examples: In current Delphi docs, Format() is the direct pretty-print method. You do not need to invent a formatter pipeline just to indent JSON.
  • Expecting key sorting from the built-in formatter: Pretty printing changes whitespace, not object semantics. If you need stable alphabetical key ordering for diffs, you must reorder properties before serialization.
  • Formatting invalid JSON: Formatters are not repair tools. Parse first, handle errors, then serialize the parsed structure.
  • Pretty-printing in production hot paths: Readable JSON is larger and slower to produce. Use it for logs, diagnostics, and human-facing exports, not every network hop.

When This Matters Most

JSON formatting pays off most when you are debugging REST responses, storing readable fixture files, troubleshooting webhook payloads, or logging intermediate data during Pascal development. In those cases, readable structure is worth the extra bytes.

For machine-to-machine transport, prefer compact JSON from ToJSON or AsJSON, and only pretty-print the payload when a human actually needs to read it.

Official References

Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool