HOW TO: Write TJSONArray or TJSONObject to a text file lazarus / 21-General ---------------------------------------------------------------------- Marion · 2021-05-07 22:30 I am studying the fpjson unit but I can't see how to read and write the json to disk? ---------------------------------------------------------------------- lainz · 2021-05-08 01:45 Using a tstringlist. Create a tstringlist assign the json text and save it to file. I recommend you jsontools, is faster. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-05-08 05:42 Hey Marion, Quote from: Marion on May 08, 2021, 12:30:36 amI 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetJSONData(const aJSON: UTF8String): TJSONData;var  jParser: TJSONParser;begin  Result := nil;{$IF FPC_FULLVERSION >= 30002}  jParser := TJSONParser.Create(aJSON, [joUTF8, joIgnoreTrailingComma]);{$ELSE}  jParser := TJSONParser.Create(aJSON, True);{$ENDIF}  try    Result := jParser.Parse;  finally    jParser.Free;  end;end; function GetJSONData(const aStream: TStream): TJSONData;var  jParser: TJSONParser;begin  Result:= nil;  aStream.Position:= 0;{$IF FPC_FULLVERSION >= 30002}  jParser:= TJSONParser.Create(aStream, [joUTF8, joIgnoreTrailingComma]);{$ELSE}  jParser := TJSONParser.Create(aStream, True);{$ENDIF}  try    Result:= jParser.Parse;  finally    jParser.Free;  end;end; This makes it easy to then do this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  stringStream: TStringStream;  jsonRoot: TJSONData;begin  // Using GetJSONData with a String  stringStream:= TStringStream.Create('', TEncoding.UTF8);  stringStream.LoadFromFile('/path/to/the/JSON/file');  jsonRoot:= GetJSONData(stringStream.UnicodeDataString);  stringStream.Freeend;or this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  fileStream: TFileStream;  jsonRoot: TJSONData;begin  // Using GetJSONData with a Stream  fileStream:= TFileStream.Create('/path/to/the/JSON/file', fmOpenRead);  jsonRoot:= GetJSONData(fileStream);  fileStream.Freeend; If you then want to save: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  stringStream: TStringStream;  jsonRoot: TJSONData;begin  stringStream:= TStringStream.Create(jsonRoot.AsJson, TEncoding.UTF8);  stringStream.SaveToFile('/path/to/the/JSON/file');  stringStream.Freeend; Hope this helps get you started. Cheers, Gus ---------------------------------------------------------------------- Marion · 2021-05-09 19:36 Thanks ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-05-09 22:07 Hey Marion, Quote from: Marion on May 09, 2021, 09:36:54 pmThanks You're more than welcome. Cheers, Gus