How do I pretty-print JSON in Delphi? – Stack Overflow
Posted by jpluimers on 2021/02/09
For my archive: the [WayBack] How do I pretty-print JSON in Delphi? – Stack Overflow answer by [WayBack] Bob:
If you do not want to use any external library, and you’re using a Delphi XE5 or newer, there is a very handy
TJson.Format()
function in theREST.Json
unit.uses json, REST.Json; { ... } function FormatJSON(json: String): String; var tmpJson: TJsonObject; begin tmpJson := TJSONObject.ParseJSONValue(json); Result := TJson.Format(tmpJson); FreeAndNil(tmpJson); end;
–jeroen
BigBother said
I don’t know with older Delphi versions, but with Delphi 11 tmpJSON.Format will do the trick (REST.Json.Format calls TJONObject.Format internally)
jpluimers said
Thanks. Unsurprisingly, only the other overload of System.JSON.TJSONObject.Format is documented: https://docwiki.embarcadero.com/Libraries/Sydney/en/System.JSON.TJSONObject.Format