JSON parser for VMS PASCAL Itanium

Post Reply

Topic author
firdousib91
Visitor
Posts: 2
Joined: Fri Apr 30, 2021 8:01 pm
Reputation: 0
Status: Offline

JSON parser for VMS PASCAL Itanium

Post by firdousib91 » Fri Apr 30, 2021 8:03 pm

Hello,

I am going to receive a JSON file/message from RestfulAPI. I would like to parse the JSON in PASCAL for processing.

Does anyone know of library that I can use?

Appreciate your help.

User avatar

martinv
Active Contributor
Posts: 36
Joined: Fri Jun 14, 2019 11:05 pm
Reputation: 0
Location: Goslar, Germany
Status: Offline
Contact:

Re: JSON parser for VMS PASCAL Itanium

Post by martinv » Sat May 01, 2021 11:34 am

There has been a thread about this a few months ago, see here. With Pascal declarations of the routines and perhaps a thin wrapper to convert strings any of the mentioned C libraries should be usable.
Never underestimate the power of human stupidity.
(Lazarus Long, by courtesy of Robert A. Heinlein)

User avatar

arne_v
Active Contributor
Posts: 33
Joined: Fri Apr 17, 2020 7:31 pm
Reputation: 0
Location: Rhode Island, USA
Status: Offline
Contact:

Re: JSON parser for VMS PASCAL Itanium

Post by arne_v » Sun May 02, 2021 4:01 pm

I gave it a try.

How does this look?

Code: Select all

[inherit('pJSON')]
program test(input, output);

const
   JSON = '[ { "no" : 1, "name" : "Alan A", "role" : "Manager" }, ' +
            '{ "no" : 2, "name" : "Brian B", "role" : "Engineer" }, ' +
            '{ "no" : 3, "name" : "Chris C", "role" : "Sales rep" } ]';




var
   employees, employee, newemployee : cJSON_ptr;
   i, no : integer;
   s, name, role : pstr;

begin
   (* iterate over all employees *)
   employees := pJSON_Parse(JSON);
   for i := 0 to pJSON_GetArraySize(employees) - 1 do begin
      employee := pJSON_GetArrayItem(employees, i);
      (* find no, name and role *)
      no := pJSON_IntValue(pJSON_GetObjectItem(employee, 'no'));
      name := pJSON_StringValue(pJSON_GetObjectItem(employee, 'name'));
      role := pJSON_StringValue(pJSON_GetObjectItem(employee, 'role'));
      (* print *)
      writeln('no = ', no:1);
      writeln('name = ', name);
      writeln('role = ', role);
   end;
   (* add employee *)
   newemployee := pJSON_CreateObject;
   pJSON_AddItemToObject(newemployee, 'no',  pJSON_CreateNumber(4));
   pJSON_AddItemToObject(newemployee, 'name',  pJSON_CreateString('Dave D'));
   pJSON_AddItemToObject(newemployee, 'role',  pJSON_CreateString('Intern'));
   pJSON_AddItemReferenceToArray(employees, newemployee);
   (* write out *)
   s := pJSON_PrintUnformatted(employees);
   writeln(s);
   (* *)
   pJSON_Delete(employees);
end.
no = 1
name = Alan A
role = Manager
no = 2
name = Brian B
role = Engineer
no = 3
name = Chris C
role = Sales rep
[{"no":1,"name":"Alan A","role":"Manager"},{"no":2,"name":"Brian B","role":"Engineer"},{"no":3,"name":"Chris C","role":"Sales rep"}, {"no":4,"name":"Dave D","role":"Intern"}]
If interest then I could make the pJSON.pas available.
Arne
arne@vajhoej.dk
VMS user since 1986


Topic author
firdousib91
Visitor
Posts: 2
Joined: Fri Apr 30, 2021 8:01 pm
Reputation: 0
Status: Offline

Re: JSON parser for VMS PASCAL Itanium

Post by firdousib91 » Mon May 03, 2021 4:09 am

Hello arne_v,

Yes, please send me the pJSON.pas file.
Thank you for the suggestion. Does this psJSON.pas also handle nested JSON file?

User avatar

arne_v
Active Contributor
Posts: 33
Joined: Fri Apr 17, 2020 7:31 pm
Reputation: 0
Location: Rhode Island, USA
Status: Offline
Contact:

Re: JSON parser for VMS PASCAL Itanium

Post by arne_v » Mon May 03, 2021 9:55 am

https://www.vajhoej.dk/arne/opensource/ ... c-v0_1.zip

cJSON supports what it supports.

I have made Pascal stubs for most of the functions, but there are still a few missing.

If you need some of the missing stuff just ping me and I will see if I can add it.
Arne
arne@vajhoej.dk
VMS user since 1986

Post Reply