Cant extract destinations from this json

Hello,

Since a couple of weeks we are learning prolog on school. Now our excercise is to create a chatbot and use an api. I choose to use an api of the NS(public transport in the netherlands). Now i wrote a little bit of prolog to get the data and read it out. Now i want to print all destinations, but i cant work it out.

This is my json: { "links": { "disruptions": { "uri": "/api/v2/disruptions?station - Pastebin.com

:- use_module(library('http/http_client')).
:- use_module(library('http/json')).
:- use_module(library('http/http_open')).

% Get the departure data of ns api on the selected trainstation
data_departures(CityCode, Data) :-
        First = 'https://ns-api.nl/reisinfo/api/v2/departures?maxJourneys=25&lang=nl&station=',
        Second = CityCode,
        atomic_concat(First, Second, HREF),
        setup_call_cleanup(
                http_open(HREF, In, [request_header('Accept'='application/json'), request_header('x-api-key'='secret')]),
                json_read_dict(In, Data),
                close(In)
        ).

departure_stations(Data, Test, Dest) :-
        Payload = Data.get(payload),
        Test = Payload.get(source),
        Departures = Payload.get(departures),
        Dest = Departures.get(destination).

And my error:

?- data_departures('LW', X), departure_stations(X, Y, Z).
ERROR: Type error: `atom' expected, found `get(destination)' (a compound)
ERROR: In:
ERROR:   [12] throw(error(type_error(atom,...),_4428))
ERROR:   [10] '$dicts':'.'('<garbage_collected>','<garbage_collected>',_4468) at c:/program files/swipl/boot/dicts.pl:46
ERROR:    [9] departure_stations('<garbage_collected>','<garbage_collected>',_4496) at c:/users/wesley/google drive/jaar 3 - ase minor/kunstmatige intelligentie/eindopdracht_chatbot/test.pl:20
ERROR:    [7] <user>
ERROR: 
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
?- 

Can someone help or give me a tip in the right way so i can work it out.

Thanks in advance,

Greetings Wesley

I like it that your exercises deal with real world tasks! If you use

?- data_departures('LW', X), gtrace, departure_stations(X, Y, Z).

and you step through the extractions, you’ll see that Departures is a list rather than a dict. I agree the error message is rather misleading. It is the result of ‘.’/2 that tries to convert the list into a dict, but the list is not suitable for this. I guess you can work out from there on how to continue.

Note that the destination dicts do not contain a destination, only a direction.

departure_stations(Data, Test, Dest) :-
Payload = Data.get(payload),
Test = Payload.get(source),
Departures = Payload.get(departures),
member(‘destination’=Dest, Departures).

Now i used member. Because what i learned with member is that you can see if the element is in the list. But now i get false.

The Departures list is a list of dict structures, not Name=Value So, you get

member(Departure, Departures)

Then you have again a dict with a number of properties.

Thanks for the answer. It works now. I can now work further on the chatbot!