How can I print JSON in Delphi?

I am looking for a function that will input a JSON string as input and format it using line breaks and indentation (tabs).

Example: I have an input line:

{"menu": {"header": "JSON viewer", "items": [{"id": "Delphi"},{"id": "Pascal", "label": "Nice tree format"}, null]}} 

And I want to get a readable result in the form of text:

 { "menu":{ "header":"JSON viewer", "items":[ { "id":"Delphi" }, { "id":"Pascal", "label":"Nice tree format" }, null ] } } 

I found many examples for PHP and C #, but not for Delphi. Can anyone help with this feature?

Update - solution with SuperObject:

 function FormatJson (InString: WideString): string; // Input string is "InString" var Json : ISuperObject; begin Json := TSuperObject.ParseString(PWideChar(InString), True); Result := Json.AsJson(true, false); //Here comes your result: pretty-print JSON end; 
+4
source share
4 answers

Use a superobject library, make sure that you are using the latest version from the repository file, not the 1.2.4 ZIP .

You can then format the TSuperObject with .AsJSON(true) ("true" does the trick).

[Note that you do not control the order in which JSON fields are displayed]

[And to create your object from a string: var lJSON : ISuperObject; lJSON := SO(string); var lJSON : ISuperObject; lJSON := SO(string); ]

+4
source

If you do not want to use an external library and use delphi XE5 or newer, there is a very convenient function TJson.Format() in REST.Json .

 uses json, REST.Json; { ... } function FormatJSON(json: String): String; var tmpJson: TJsonObject; begin tmpJson := TJSONObject.ParseJSONValue(json); Result := TJson.Format(tmpJson); FreeAndNil(tmpJson); end; 
+5
source

You can also use the following methods of our Open Source SynCommons.pas :

 var json,new: RawUTF8; begin json := '{"menu": {"header": "JSON viewer", "items": [{"id": "Delphi"},{"id": "Pascal", "label": "Nice tree format"}, null]}}'; new := JSONReformat(json,jsonHumanReadable); ... 

Here new will contain:

 { "menu": { "header": "JSON viewer", "items": [ { "id": "Delphi" }, { "id": "Pascal", "label": "Nice tree format" }, null ] } } 

If you are using jsonUnquotedPropName format:

  new := JSONReformat(json,jsonUnquotedPropName); 

You will get the following extended syntax (similar to the one used in JavaScript or the MongoDB shell):

 { menu: { header: "JSON viewer", items: [ { id: "Delphi" }, { id: "Pascal", label: "Nice tree format" }, null ] } } 

This syntax is accepted as valid input for all JSON functions in our Open Source framework, as an alternative to the default JSON syntax. We found this quite useful, for example. for configuration files.

Please note that our JSONReformat() function is very fast. It converts a massive 190 MB of non-convertible JSON content from CityLots to 400 MB decorated with JSON (intended and with margins) in 1.4 seconds. SuperObject is simply capable of reading it in 10 seconds and uses 1.1 GB only to store 190 MB of content. And DBXJSON cannot even load data: it consumes all 32-bit memory - under Win64 (XE6), takes 50 seconds and uses 3 GB of RAM to read 190 MB JSON. See this article for some numbers .

+2
source

If you are working with Delphi XE or later, you can use the delphi-xe-json library

 function PrettyPrint (aJSON : string) : string; var jo : IJSONObject begin jo := TJSON.NewObject(aJSON); result := jo.ToString(true); end; 
+1
source

Source: https://habr.com/ru/post/1499619/


All Articles