Recent

Author Topic: HOW TO: Write TJSONArray or TJSONObject to a text file  (Read 2430 times)

Marion

  • Full Member
  • ***
  • Posts: 122
HOW TO: Write TJSONArray or TJSONObject to a text file
« on: May 08, 2021, 12:30:36 am »
I am studying the fpjson unit but I can't see how to read and write the json to disk?
Thank you,
Marion
(A recovering Windows programmer.)

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: HOW TO: Write TJSONArray or TJSONObject to a text file
« Reply #1 on: May 08, 2021, 03:45:06 am »
Using a tstringlist. Create a tstringlist assign the json text and save it to file.

I recommend you jsontools, is faster.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1088
  • Professional amateur ;-P
Re: HOW TO: Write TJSONArray or TJSONObject to a text file
« Reply #2 on: May 08, 2021, 07:42:10 am »
Hey Marion,

I am studying the fpjson unit but I can't see how to read and write the json to disk?

I have these 2 function that I use to get my TJSONData objects:
Code: Pascal  [Select][+][-]
  1. function GetJSONData(const aJSON: UTF8String): TJSONData;
  2. var
  3.   jParser: TJSONParser;
  4. begin
  5.   Result := nil;
  6. {$IF FPC_FULLVERSION >= 30002}
  7.   jParser := TJSONParser.Create(aJSON, [joUTF8, joIgnoreTrailingComma]);
  8. {$ELSE}
  9.   jParser := TJSONParser.Create(aJSON, True);
  10. {$ENDIF}
  11.   try
  12.     Result := jParser.Parse;
  13.   finally
  14.     jParser.Free;
  15.   end;
  16. end;
  17.  
  18. function GetJSONData(const aStream: TStream): TJSONData;
  19. var
  20.   jParser: TJSONParser;
  21. begin
  22.   Result:= nil;
  23.   aStream.Position:= 0;
  24. {$IF FPC_FULLVERSION >= 30002}
  25.   jParser:= TJSONParser.Create(aStream, [joUTF8, joIgnoreTrailingComma]);
  26. {$ELSE}
  27.   jParser := TJSONParser.Create(aStream, True);
  28. {$ENDIF}
  29.   try
  30.     Result:= jParser.Parse;
  31.   finally
  32.     jParser.Free;
  33.   end;
  34. end;

This makes it easy to then do this:
Code: Pascal  [Select][+][-]
  1. var
  2.   stringStream: TStringStream;
  3.   jsonRoot: TJSONData;
  4. begin
  5.   // Using GetJSONData with a String
  6.   stringStream:= TStringStream.Create('', TEncoding.UTF8);
  7.   stringStream.LoadFromFile('/path/to/the/JSON/file');
  8.   jsonRoot:= GetJSONData(stringStream.UnicodeDataString);
  9.   stringStream.Free
  10. end;
or this:
Code: Pascal  [Select][+][-]
  1. var
  2.   fileStream: TFileStream;
  3.   jsonRoot: TJSONData;
  4. begin
  5.   // Using GetJSONData with a Stream
  6.   fileStream:= TFileStream.Create('/path/to/the/JSON/file', fmOpenRead);
  7.   jsonRoot:= GetJSONData(fileStream);
  8.   fileStream.Free
  9. end;

If you then want to save:
Code: Pascal  [Select][+][-]
  1. var
  2.   stringStream: TStringStream;
  3.   jsonRoot: TJSONData;
  4. begin
  5.   stringStream:= TStringStream.Create(jsonRoot.AsJson, TEncoding.UTF8);
  6.   stringStream.SaveToFile('/path/to/the/JSON/file');
  7.   stringStream.Free
  8. end;

Hope this helps get you started.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Marion

  • Full Member
  • ***
  • Posts: 122
Re: HOW TO: Write TJSONArray or TJSONObject to a text file
« Reply #3 on: May 09, 2021, 09:36:54 pm »
Thanks
Thank you,
Marion
(A recovering Windows programmer.)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1088
  • Professional amateur ;-P
Re: HOW TO: Write TJSONArray or TJSONObject to a text file
« Reply #4 on: May 10, 2021, 12:07:56 am »
Hey Marion,

Thanks

You're more than welcome.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018