Sign in Join

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

Mailing lists / fpc-pascal / 2023-06

Parse JSON

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Darius Blaszyk

Hi all,

I've hardly done anything with JSON in Freepascal before, so apologies for the trivial question. I want to traverse an array in JSON and retrieve the elements (which I don't know what they are in advance) one by one. An example of the JSON file is given below.

"keywords": {
"key": "value",
"key1": "value1",
"key2": "value2"
},

Ideally, I'd like to be able to stick to the FPC out-of-the-box library. Any help would be appreciated.

Rgds, Darius

Reply by email

Thomas Kurz
to Darius Blaszyk

fpJson fully supports JSON:

https://www.freepascal.org/docs-html/fcl/fpjson/index.html https://wiki.lazarus.freepascal.org/JSON

There's also JSON support in the LGenerics package, but it's not a bundled package as far as I know.

Thomas

Earlier message 23 lines
----- Original Message ----- From: Darius Blaszyk via fpc-pascal To: FPC-Pascal users discussions Sent: Friday, June 23, 2023, 16:02:26 Subject: [fpc-pascal] Parse JSON Hi all, I've hardly done anything with JSON in Freepascal before, so apologies for the trivial question. I want to traverse an array in JSON and retrieve the elements (which I don't know what they are in advance) one by one. An example of the JSON file is given below. "keywords": { "key": "value", "key1": "value1", "key2": "value2" }, Ideally, I'd like to be able to stick to the FPC out-of-the-box library. Any help would be appreciated. Rgds, Darius

Reply by email

Michael Van Canneyt
to Darius Blaszyk: I've hardly done anything with JSON in Freepascal before, so apologies for the trivial…
Darius Blaszyk wrote 16 lines
Hi all, I've hardly done anything with JSON in Freepascal before, so apologies for the trivial question. I want to traverse an array in JSON and retrieve the elements (which I don't know what they are in advance) one by one. An example of the JSON file is given below. "keywords": { "key": "value", "key1": "value1", "key2": "value2" }, Ideally, I'd like to be able to stick to the FPC out-of-the-box library. Any help would be appreciated.

Something like:

uses classes, fpjson;
var
   J : TJSONObject;
   F : TFileStream;
begin
   J:=nil;
   F:=TFileStream.Create('yourfile.json',fmOpenRead);
   try
     J:=GetJSON(F) as TJSONObject;
     For I:=0 to J.Count-1 do
       Writeln(J.Names[i],' : ',J.AsString);
   finally
     J.Free;
     F.Free;
   end;
end.

Reply by email