unit writedatasetJSONFormat_helper; {$mode objfpc}{$H+} interface uses Classes, SysUtils, DB, SQLDB, jsonscanner, jsonparser, fpsimplejsonexport; type TQueryDataToJSON = class helper for TSQLQuery private public function WriteJSONFormat : string; end; implementation function TQueryDataToJSON.WriteJSONFormat: string; var LStream: TStringStream; LJSONExp: TSimpleJSONExporter; LParser : TJSONParser; memText : string; begin LStream := TStringStream.Create(''); LJSONExp := TSimpleJSONExporter.Create(nil); with LJSONExp do begin FormatSettings.DateFormat := 'YYYY-MM-DD'; FormatSettings.DateTimeFormat := 'YYYY-MM-DD hh:mm:ss'; FormatSettings.DecimalSeparator := '.'; FormatSettings.IndentSize:= 5; FormatSettings.ColumnFormat := cfObject; FormatSettings.RowFormat := rfArray; FileName := 'ConvertJSON'; end; // try try LJSONExp.Dataset := Self; LJSONExp.ExportToStream(LStream); except on Exception do begin FreeAndNil(LJSONExp); end; end; finally memText := StringReplace(LStream.DataString, ';', ',', [rfReplaceAll]); memText := concat(LeftStr(memText, Length(memText) - 6),']'); LParser:=TJSONParser.Create(memText, [joUTF8]); Result := LParser.Parse.FormatJSON([], 2); end; end; end.