How to get a value from a field in a Json string in Prolog?

691 views Asked by At

I am working on a Prolog project that can answer questions about Movies and Series from the omdb api.

I use this to return the complete Json string with information of a Movie or Series:

findMovie(X,Json):-
    atomic_list_concat(X, ',', Atom),
    uri_query_components(QS, [t=Atom]) %t is the title of the movie
    format(atom(HREF),'http://www.omdbapi.com/?~s',[QS]),
    http_get(HREF,Json, []),
    write(Json).

If I search for example on: "Fantastic Beasts", write(Json) will print the following in the console:

{"Title":"Fantastic Beasts and Where to Find Them",
"Year":"2016",
"Rated":"PG-13",
"Released":" 2016",
"Runtime":"133 min",
"Genre":"Adventure, Family, Fantasy",
"Director":"David Yates","WriJ.K. Rowling",
"Actors":"Eddie Redmayne, Sam Redford, Scott Goldman, Tim Bentinck",
"Plot":"Thetures of writer Newt Scamander in New York's secret community of witches and wizards seventy before Harry Potter reads his book in school.",
"Language":"English",
"Country":"UK, USA","Awards":"1 nomination.",
"Poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BMjMxOTM1OTI4MV5BBnXkFtZTgwODE5OTYxMDI@._V1_SX300.jpg",
"Metascore":"66",
"imdbRating":"7.9",
"imdbVotes":"75,816bID":"tt3183660",
"Type":"movie",
"Response":"True"}

How can I return a value? For example: the value of "Year" which is 2016. I've read some things about converting the Json string to a Prolog format but I couldn't figure it out.

1

There are 1 answers

0
Janco de Vries On BEST ANSWER

I found the solution for this.

findMovie(X,Json):-
    Field = 'Year',
    atomic_list_concat(X, ',', Atom),
    uri_query_components(QS, [t=Atom]) %t is the title of the movie
    format(atom(HREF),'http://www.omdbapi.com/?~s',[QS]),
    http_get(HREF,json(Json), []),   %json(Json) converts it to Prolog terms.
    member(Field=Result,Json),    %Result will get the value of 'Year'
    write(Result).