Converting lists to JSON using json_object

Hi I am having problems converting lists to Json.
this is my code:

:- json_object schedule_result(room:string, day:string, room_agenda:list(schedule_segment), lnaesthetist:list(schedule_doctors), ldoctors:list(schedule_doctors), lcleaners:list(schedule_doctors), stime:integer).
:- json_object schedule_doctors(doctor:string, doctor_agenda:list(schedule_segment)).
:- json_object schedule_segment(start=integer, end=integer, opcode=string).

:- http_handler('/bestsolution', handle_get_better_solution, [ ]).
handle_get_better_solution(Request) :-
http_read_json(Request, JSON, [json_object(dict)]),
Room =  JSON.room,
Day = JSON.day,
obtain_better_sol(or1,20241028,LRooms,LAnesthetist,LDoctors,LCleaners,Time),
convert_segment_list_to_json(LRooms, JsonLRooms),
convert_doctors_list_to_json(LAnesthetist, JsonLAnesthetist),
convert_doctors_list_to_json(LDoctor, JsonLDoctor),
convert_doctors_list_to_json(LCleaner, JsonLCleaner),

R = schedule_result(Room, Day, JsonLRooms, JsonLAnesthetist, JsonLDoctor,JsonLCleaner, Time),
prolog_to_json(R, JSONObject),
reply_json(JSONObject, [json_object(dict)]).


convert_segment_list_to_json([], []).
convert_segment_list_to_json([(Start, End, OpCode)|Tail], [Reply | JsonArray]) :-
    Reply = schedule_segment(Start, End, OpCode),
    convert_segment_list_to_json(Tail, JsonArray).

convert_doctors_list_to_json([], []).
convert_doctors_list_to_json([(Doctor, Agenda)| Tail], [Reply | JsonArray]) :-
    convert_segment_list_to_json(Agenda, JsonAgenda),
    Reply = schedule_doctors(Doctor, JsonAgenda),
    convert_doctors_list_to_json(Tail, JsonArray).

I tried changing the json_objects to this:

:- json_object schedule_result(room:string, day:string, room_agenda:list(schedule_segment), lnaesthetist:string, ldoctors:string, lcleaners:string, stime:integer).
:- json_object schedule_doctors(doctor:string, doctor_agenda:list(schedule_segment)).
:- json_object schedule_segment(start=integer, end=integer, opcode=string).

for debugging and the room_agenda was working. The problem is when i trt to use list(schedule_doctors).
When I try it i receive this error message:

{
    "code": 500,
    "message": "Type error: `json_term' expected, found `schedule_doctors(a003,[schedule_segment(1060,1179,so100003)])' (a compound)

You are mixing the conversion based on old style JSON handling with handling JSON as dicts. That is probably not really the reason for the error though. Using e.g.

seems wrong. Must use : if this is a type. As the code is not complete, so we cannot run it and we just have an error message to work with, it is hard to tell what exactly goes wrong. There are some ways to isolate the issue

  • load library(http/http_error). That causes a stack trace to be attached to the error, so you have a much better insight.
  • Use ?- tspy(handle_get_better_solution)., which should bring up the debugger when issuing the request.
  • Use ?- gtrap(type_error(_,_)), tdebug. to trap the debugger when a type error occurrs.

Find the goal that goes wrong and make a minimal example. You probably understand what is wrong and if not, you can ask a question that is easier to answer for people here. Preferably, turn it into something people can run.