Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-01

Formating JSON outputs

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

luciano de souza

Hello all,

JSON is a very easy format to handle with Freepascal. However, the output is not so readable when the number of registers increase. An example of it is:

program test;
{$mode objfpc}{$H+}
uses
classes, fpjson;
var
r, j: TJSONObject;
a: TJSONArray;
s: TStringlist;
BEGIN
s := TStringlist.create;
r := TJSONObject.create;
a := TJSONarray.create;
j := TJsonObject.create;
j.strings['name'] := 'Luciano de Souza';
j.integers['age'] := 38;
j.strings['city'] := 'Niterói';
j.strings['country'] := 'Brazil';
a.add(j.clone);
j.clear;
j.strings['name'] := 'Mariella Molinari';
j.integers['age'] := 29;
j.strings['city'] := 'Padova';
j.strings['country'] := 'Italy';
a.add(j.clone);
r.add('people', a);
s.text := r.AsJson;
s.SavetoFile('test.txt');
try
finally
j.free;
r.free;
s.free;
end;
END.

The corresponding output is:

{ "people" : [{ "name" : "Luciano de Souza", "age" : 38, "city" : "Niterói", "country" : "Brazil" }, { "name" : "Mariella Molinari", "age" : 29, "city" : "Padova", "country" : "Italy" }] }

My question is: is there a way to automatically format it? Breaking lines? Becoming easy the reading?

Luciano de Souza
Sven Barth
to luciano de souza
luciano de souza wrote 5 lines
Hello all, JSON is a very easy format to handle with Freepascal. However, the output is not so readable when the number of registers increase. An example of it is:

[snip]

s.text := r.AsJson; s.SavetoFile('test.txt');

[snip]

Show quoted text 8 lines
The corresponding output is: { "people" : [{ "name" : "Luciano de Souza", "age" : 38, "city" : "Niterói", "country" : "Brazil" }, { "name" : "Mariella Molinari", "age" : 29, "city" : "Padova", "country" : "Italy" }] } My question is: is there a way to automatically format it? Breaking lines? Becoming easy the reading?

TJSONData.AsJSON basically uses the format options "foSingleLineArray, foSingleLineObject" (it's not implemented that way though, put it appears like this ^^). To manually influence this you should take a look at TJSONData.FormatJSON.

Regards,
Sven

Michael Van Canneyt
to luciano de souza: JSON is a very easy format to handle with Freepascal. However, the output is not so…
luciano de souza wrote 6 lines
Hello all, JSON is a very easy format to handle with Freepascal. However, the output is not so readable when the number of registers increase. An example of it is:

[snip]

Show quoted text 8 lines
The corresponding output is: { "people" : [{ "name" : "Luciano de Souza", "age" : 38, "city" : "Niterói", "country" : "Brazil" }, { "name" : "Mariella Molinari", "age" : 29, "city" : "Padova", "country" : "Italy" }] } My question is: is there a way to automatically format it? Breaking lines? Becoming easy the reading?

Yes. Use the FormatJSON function.
Note that it is slower than the AsJSON function, because of the formatting.

Michael.