Read JSON from file

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Read JSON from file

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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

leledumbo
Administrator
> 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
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

Felipe Monteiro de Carvalho
In reply to this post by Chris Moody
On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody
<[hidden email]> 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.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

Michael Van Canneyt
In reply to this post by Chris Moody


On Thu, 6 Aug 2015, Chris Moody wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

Michael Van Canneyt
In reply to this post by Felipe Monteiro de Carvalho


On Fri, 7 Aug 2015, Felipe Monteiro de Carvalho wrote:

> On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody
> <[hidden email]> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

Michael Van Canneyt


On Fri, 7 Aug 2015, Michael Van Canneyt wrote:

>
>
> On Fri, 7 Aug 2015, Felipe Monteiro de Carvalho wrote:
>
>> On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody
>> <[hidden email]> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

Chris Moody
In reply to this post by leledumbo
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?

Chris

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

Michael Van Canneyt


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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

Chris Moody


On 08/07/2015 12:46 AM, Michael Van Canneyt wrote:

>
>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

Felipe Monteiro de Carvalho
In reply to this post by Michael Van Canneyt
On Fri, Aug 7, 2015 at 8:33 AM, Michael Van Canneyt
<[hidden email]> wrote:
> 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.

Great! =)

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Read JSON from file

leledumbo
Administrator
In reply to this post by Chris Moody
> J:=GetJSON(json);    // Error is here.

quick fix:

J:=GetJSON(json.text);