I am looking for a function that will take a string of JSON as input and format it with line breaks and indentations (tabs).
Example: I have input line:
You may also use the following methods of our Open Source SynCommons.pas
unit:
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 use the jsonUnquotedPropName
format:
new := JSONReformat(json,jsonUnquotedPropName);
you will get the following extended syntax (similar to the one used in JavaScript or 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 the JSON functions of our Open Source framework, as alternative to the default JSON syntax. We found it pretty useful, e.g. for configuration files.
Note that our JSONReformat()
function is very fast. It converts the huge 190 MB of unconformatable JSON content from CityLots into 400 MB of beautified JSON (intended and with line fields) in 1.4 seconds. SuperObject is just able to read it in 10 seconds, and uses 1.1 GB just for storing the 190 MB of content. And DBXJSON is not even able to load the data: it consumes all 32 bit memory - under Win64 (XE6), it takes 50 seconds and uses 3 GB of RAM to read the 190 MB of JSON. See this article for some numbers.