Sign in Join

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

Mailing lists / fpc-pascal / 2015-08

Read JSON from file

11 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Chris Moody

Hi all,

For my current project, I download a file from a server that contains JSON code. I'm not sure how to read it into something that GetJSON is able to handle.

My first thought was using TStrings, however not sure how to convert a TString into TStream.

Any assistance would be much appreciated.

Chris

Reply by email

Felipe Monteiro de Carvalho
to Chris Moody: For my current project, I download a file from a server that contains JSON code. I'm not…
Chris Moody wrote 6 lines
For my current project, I download a file from a server that contains JSON code. I'm not sure how to read it into something that GetJSON is able to handle. My first thought was using TStrings, however not sure how to convert a TString into TStream.

I do it like this, with TStringStream:

  lStrings := TStringList.Create;
  try
    lStrings.LoadFromFile(AFile);
    // Parse JSON data
    lStream := TStringStream.Create(lStrings.Text);
    lParser := TJSONParser.Create(lStream);
    try
      lParser.Strict := False;
      lData := lParser.Parse;
    finally
      lParser.Free;
      lStream.Free;
    end;

Well, in my particular case I use TStringList because I pre-process the data, removing comments which are not allowed in JSON.

Felipe Monteiro de Carvalho

Reply by email

Michael Van Canneyt
to Chris Moody: For my current project, I download a file from a server that contains JSON code. I'm not…
Chris Moody wrote 10 lines
Hi all, For my current project, I download a file from a server that contains JSON code. I'm not sure how to read it into something that GetJSON is able to handle. My first thought was using TStrings, however not sure how to convert a TString into TStream. Any assistance would be much appreciated.

The following will read JSON and write all keys it finds

uses jsonparser, fpjson, sysutils, classes;
Var
   F : TFileStream;
   D : TJSONData;
   E : TJSONEnum;
begin
   D:=Nil;
   F:=TFileStream.Create('myfile.json',fmOpenRead or fmShareDenywrite);
   try
     D:=GetJSON(F);
     for E in D do
       Writeln(E.Key, ' : ',E.Value.AsJSON);
   Finally
     D.free;
     F.Free;
   end;
end.

Michael.

Reply by email

Michael Van Canneyt
to Felipe Monteiro de Carvalho: On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody I do it like this, with TStringStream:…
Felipe Monteiro de Carvalho wrote 28 lines
On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody wrote: > For my current project, I download a file from a server that contains JSON > code. I'm not sure how to read it into something that GetJSON is able to > handle. > > My first thought was using TStrings, however not sure how to convert a > TString into TStream. I do it like this, with TStringStream: lStrings := TStringList.Create; try lStrings.LoadFromFile(AFile); // Parse JSON data lStream := TStringStream.Create(lStrings.Text); lParser := TJSONParser.Create(lStream); try lParser.Strict := False; lData := lParser.Parse; finally lParser.Free; lStream.Free; end; Well, in my particular case I use TStringList because I pre-process the data, removing comments which are not allowed in JSON.

As it happens, I have yesterday committed an extension which allows you to specify that the JSON supports comments. Both // and /* */ are detected and discarded.

Michael.

Reply by email

Michael Van Canneyt
to Michael Van Canneyt
Michael Van Canneyt wrote 35 lines
On Fri, 7 Aug 2015, Felipe Monteiro de Carvalho wrote: > On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody > wrote: >> For my current project, I download a file from a server that contains JSON >> code. I'm not sure how to read it into something that GetJSON is able to >> handle. >> >> My first thought was using TStrings, however not sure how to convert a >> TString into TStream. > > I do it like this, with TStringStream: > > lStrings := TStringList.Create; > try > lStrings.LoadFromFile(AFile); > > // Parse JSON data > lStream := TStringStream.Create(lStrings.Text); > lParser := TJSONParser.Create(lStream); > try > lParser.Strict := False; > lData := lParser.Parse; > finally > lParser.Free; > lStream.Free; > end; > > Well, in my particular case I use TStringList because I pre-process > the data, removing comments which are not allowed in JSON. As it happens, I have yesterday committed an extension which allows you to specify that the JSON supports comments.

s/supports/contains/ , obviously.

Michael.

Reply by email

Chris Moody
to leledumbo: to read it into something that GetJSON is able to handle. Can't you figure out from the…
leledumbo wrote 7 lines
> I download a file from a server that contains JSON code. I'm not sure how to read it into something that GetJSON is able to handle. Can't you figure out from the function interface: http://www.freepascal.org/docs-html/fcl/fpjson/getjson.html
Dentist.pas(56,18) Error: Incompatible type for arg no. 1: Got
"TStringList", expected "TStream"

This is what I get when I use:

function ReadJSON (jsonfile, node:string) : string;
var
    json: tStringlist;
    J: TJSONData;
begin
   json:=TStringList.Create;
   json.loadfromfile(jsonfile);
   J:=GetJSON(json);    // Error is here.
   ReadJSON := J.FindPath(node).AsString;
end;

Does this mean my Lazarus / FPC is out of date? Or I'm using the wrong library or something?

Chris

Reply by email

Michael Van Canneyt
to Chris Moody
Chris Moody wrote 26 lines
On 08/06/2015 07:01 PM, leledumbo wrote: >> I download a file from a server that contains JSON code. I'm not sure how > to read it into something that GetJSON is able to handle. > > Can't you figure out from the function interface: > http://www.freepascal.org/docs-html/fcl/fpjson/getjson.html > > Dentist.pas(56,18) Error: Incompatible type for arg no. 1: Got "TStringList", expected "TStream" This is what I get when I use: function ReadJSON (jsonfile, node:string) : string; var json: tStringlist; J: TJSONData; begin json:=TStringList.Create; json.loadfromfile(jsonfile); J:=GetJSON(json); // Error is here. ReadJSON := J.FindPath(node).AsString; end; Does this mean my Lazarus / FPC is out of date? Or I'm using the wrong library or something?

GetJSON doesn't allow a Tstrings argument. Where did you get that from ?

Please check the sample code I sent.

Michael.

Reply by email

Chris Moody
to Michael Van Canneyt
Michael Van Canneyt wrote 37 lines
On Fri, 7 Aug 2015, Chris Moody wrote: > On 08/06/2015 07:01 PM, leledumbo wrote: >>> I download a file from a server that contains JSON code. I'm not >>> sure how >> to read it into something that GetJSON is able to handle. >> >> Can't you figure out from the function interface: >> http://www.freepascal.org/docs-html/fcl/fpjson/getjson.html >> >> > Dentist.pas(56,18) Error: Incompatible type for arg no. 1: Got > "TStringList", expected "TStream" > > This is what I get when I use: > > function ReadJSON (jsonfile, node:string) : string; > var > json: tStringlist; > J: TJSONData; > begin > json:=TStringList.Create; > json.loadfromfile(jsonfile); > J:=GetJSON(json); // Error is here. > ReadJSON := J.FindPath(node).AsString; > end; > > Does this mean my Lazarus / FPC is out of date? Or I'm using the > wrong library or something? GetJSON doesn't allow a Tstrings argument. Where did you get that from ? Please check the sample code I sent. Michael.

I do apologize, I was looking at a web page and found I didn't read it well enough. Thanks for your code suggestion, I will see about making it work for me.

Chris

Reply by email