Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Pretty Print JSON (Formatter, Beautifier)

See more JSON Examples

Demonstrates how to emit JSON in a pretty, human-readable format with indenting of nested arrays and objects.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  json: TJsonObject;
  jsonStr: string;

begin
  success := False;

  json := TJsonObject.Create;

  jsonStr := '{"name": "donut","image":{"fname": "donut.jpg","w": 200,"h": 200},"thumbnail":{"fname": "donutThumb.jpg","w": 32,"h": 32}}';

  success := json.Load(jsonStr);
  if (success <> True) then
    begin
      WriteLn(json.LastErrorText);
      Exit;
    end;

  //  To pretty-print, set the EmitCompact property equal to False
  json.EmitCompact := False;

  //  If bare-LF line endings are desired, turn off EmitCrLf
  //  Otherwise CRLF line endings are emitted.
  json.EmitCrLf := False;

  //  Emit the formatted JSON:
  WriteLn(json.Emit());


  json.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.