Page 1 of 1
JSON parser for VMS PASCAL Itanium
Posted: Fri Apr 30, 2021 8:03 pm
by firdousib91
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.
Re: JSON parser for VMS PASCAL Itanium
Posted: Sat May 01, 2021 11:34 am
by martinv
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.
Re: JSON parser for VMS PASCAL Itanium
Posted: Sun May 02, 2021 4:01 pm
by arne_v
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.
Re: JSON parser for VMS PASCAL Itanium
Posted: Mon May 03, 2021 4:09 am
by firdousib91
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?
Re: JSON parser for VMS PASCAL Itanium
Posted: Mon May 03, 2021 9:55 am
by arne_v
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.