Help with extracting JSON data from file

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Help with extracting JSON data from file

Post by Fangbeast »

I need a little help with extracting JSON data from a file on disk. As I don't know what I am doing here at all.

I retrieved a JSON data from from OMDB using the right query and got back "OMDBAPIData.json" and got the data below but then what?

Code: Select all

{"Title":"Stargate: The Ark of Truth","Year":"2008","Rated":"Not Rated","Released":"11 Mar 2008","Runtime":"97 min","Genre":"Action, Adventure, Drama, Fantasy, Sci-Fi","Director":"Robert C. Cooper","Writer":"Robert C. Cooper","Actors":"Ben Browder, Amanda Tapping, Christopher Judge, Michael Shanks","Plot":"SG-1 searches for an ancient weapon which could help them defeat the Ori, and discover it may be in the Ori's own home galaxy. As the Ori prepare to send ships through to the Milky Way to attack Earth, SG-1 travels to the Ori galaxy aboard the Odyssey. The International Oversight committee has their own plans and SG-1 finds themselves in a distant galaxy fighting two powerful enemies.","Language":"English","Country":"USA, Canada","Awards":"N/A","Poster":"https://m.media-amazon.com/images/M/MV5BMjEyODQwMzYyM15BMl5BanBnXkFtZTcwOTY5MzI3NA@@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"7.4/10"}],"Metascore":"N/A","imdbRating":"7.4","imdbVotes":"18,866","imdbID":"tt0942903","Type":"movie","DVD":"11 Mar 2008","BoxOffice":"N/A","Production":"MGM","Website":"N/A","Response":"True"}
No experience with anything JSON so what do I do next to get it into a structure?.

Code: Select all

JsonHandle.i =  LoadJSON(#PB_Any, "OMDBAPIData.json", #PB_JSON_NoCase)

If JsonHandle.i
  Debug ComposeJSON(JsonHandle.i, #PB_JSON_PrettyPrint)
Else
  Debug "Could not open the JSON data file"  
EndIf

Amateur Radio, D-STAR/VK3HAF
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: Help with extracting JSON data from file

Post by Bisonte »

Two structures and one command are required here.
I've put something together, but without any guarantee.
I didn't find an official API result page... so how the result
should look like or in case of an error should look like.

Here I made two procedures. One to load the result from
a file, and the other to pull it directly from the web without
downloading a file.

Code: Select all

EnableExplicit

Structure sub_ratings
  Source.s
  Value.s
EndStructure
Structure omdb_result
  
  Title.s
  Year.s
  Released.s
  Runtime.s
  Genre.s
  Director.s
  Writer.s
  Actors.s
  Plot.s
  Language.s
  Country.s
  Awards.s
  Poster.s
  List Ratings.sub_ratings()
  Metascore.s
  imdbRating.s
  imdbVotes.s
  imdbID.s
  Type.s
  DVD.s
  BoxOffice.s
  Production.s
  Website.s
  Response.s
  Error.s
EndStructure
  
Procedure.i Get_OMDB_Result_From_Url(Url.s, *Result.omdb_result)
  
  Protected jSon, *Mem, Result
  
  If InitNetwork()
    
    *Mem = ReceiveHTTPMemory(Url, #PB_HTTP_NoRedirect)
    
    If *Mem
      
      jSon = CatchJSON(#PB_Any, *Mem, MemorySize(*Mem), #PB_JSON_NoCase)
      
      If jSon
        ExtractJSONStructure(JSONValue(jSon), *Result, omdb_result)
        FreeJSON(jSon)
        Result = #True
      EndIf
        
      FreeMemory(*Mem)
      
    EndIf
    
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
Procedure.i Get_OMDB_Result(File.s, *Result.omdb_result)
  
  Protected jSon, rVal = #False
  
  jSon = LoadJSON(#PB_Any, File, #PB_JSON_NoCase)
  
  If jSon
    ExtractJSONStructure(JSONValue(jSon), *Result, omdb_result)
    FreeJSON(jSon)
    rVal = #True
  EndIf
  
  ProcedureReturn rVal
  
EndProcedure

; Demo

Define Movie.omdb_result

If Get_OMDB_Result("OMDBAPIData.json", @Movie)
  If Movie\Response <> "False"   
    Debug Movie\Actors
    Debug Movie\Awards
    Debug Movie\BoxOffice
    Debug Movie\Country
    Debug Movie\Director
    Debug Movie\DVD
    Debug Movie\Genre
    Debug Movie\imdbID
    Debug Movie\imdbRating
    Debug Movie\imdbVotes
    Debug Movie\Language
    Debug Movie\Metascore
    Debug Movie\Plot
    Debug Movie\Poster
    Debug Movie\Production
    Debug Movie\Ratings()\Source
    Debug Movie\Ratings()\Value
    Debug Movie\Released
    Debug Movie\Response
    Debug Movie\Runtime
    Debug Movie\Title
    Debug Movie\Type
    Debug Movie\Website
    Debug Movie\Writer
    Debug Movie\Year
  Else
    Debug "Error : " + Movie\Error
  EndIf
EndIf
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Help with extracting JSON data from file

Post by Fangbeast »

I tried this and didn't get anything back, don't know what I missed. I can't post my own API Key in her to show you though.

Code: Select all

EnableExplicit

; My personal constants

Structure ProgramOptions
  HTTPHeader.s                                                                                                  ; 
  HTTPRequestString.s                                                                                           ; 
EndStructure

Global Program.ProgramOptions                                                                                   ; 

Program\HTTPHeader        = "http://www.omdbapi.com/?"                                                           ; 

Structure OmdbOptions
  Stitle.s                                                                                                       ; 
  Imdb.s                                                                                                         ; 
  Title.s                                                                                                        ; 
  Year.s                                                                                                         ; 
  Response.s                                                                                                     ; 
  Plot.s                                                                                                         ; 
  Tomatoes.s                                                                                                     ; 
  Type.s                                                                                                         ; 
  Apikey.s                                                                                                       ; 
EndStructure

Global Omdb.OmdbOptions                                                                                         ; 

Omdb\Stitle     = "s=" 		                                                                                       ; Search For a title
Omdb\Imdb       = "i=" 		                                                                                       ; Search for imdb movie id
Omdb\Title      = "t="	  	                                                                                     ; Title To Return
Omdb\Year       = "y="		                                                                                       ; Search for year
Omdb\Response   = "r="		                                                                                       ; Response for query (True or False)
Omdb\Plot       = "Plot="	                                                                                       ; Plot type (= short, = full) (Short Or extended plot)
Omdb\Tomatoes   = "tomatoes="                                                                                    ; Rotten Tomatoes (= true (optional) add rotten tomatoes Data
Omdb\Type.s     = "r="                                                                                           ; Data response type (JSON Or XML) (r=XML)
Omdb\Apikey     = "apikey="                                                                                      ; My single API key for requests

; http://www.omdbapi.com/?i=tt3896198&apikey=[YourKey]
; http://www.omdbapi.com/?t=stargate+continuum&y=2008&plot=full&apikey=[YourKey]

Structure sub_ratings
  Source.s
  Value.s
EndStructure

Structure omdb_result
  Title.s
  Year.s
  Released.s
  Runtime.s
  Genre.s
  Director.s
  Writer.s
  Actors.s
  Plot.s
  Language.s
  Country.s
  Awards.s
  Poster.s
  List Ratings.sub_ratings()
  Metascore.s
  imdbRating.s
  imdbVotes.s
  imdbID.s
  Type.s
  DVD.s
  BoxOffice.s
  Production.s
  Website.s
  Response.s
  Error.s
EndStructure

Procedure.i Get_OMDB_Result_From_Url(Url.s, *Result.omdb_result)
  Protected jSon, *Mem, Result
  If InitNetwork()
    *Mem = ReceiveHTTPMemory(Url, #PB_HTTP_NoRedirect)
    If *Mem
      jSon = CatchJSON(#PB_Any, *Mem, MemorySize(*Mem), #PB_JSON_NoCase)
      If jSon
        ExtractJSONStructure(JSONValue(jSon), *Result, omdb_result)
        FreeJSON(jSon)
        Result = #True
      EndIf
      FreeMemory(*Mem)
    EndIf
  EndIf
  ProcedureReturn Result
EndProcedure

Define Movie.omdb_result, Url.s

; Example using a download to memory

Program\HTTPRequestString.s   =       Program\HTTPHeader.s
Program\HTTPRequestString.s   +       Omdb\Title.s  + "pulp fiction"
Program\HTTPRequestString.s   + "&" + Omdb\Plot.s   + "full"
Program\HTTPRequestString.s   + "&" + Omdb\Type.s   + "json"
Program\HTTPRequestString.s   + "&" + Omdb\Apikey.s + "[YourKey]"                                               ; Put your own API key in here

Debug Program\HTTPRequestString.s

If Get_OMDB_Result_From_Url(Program\HTTPRequestString.s, @Movie)
  If Movie\Response <> "False"   
    Debug Movie\Actors
    Debug Movie\Awards
    Debug Movie\BoxOffice
    Debug Movie\Country
    Debug Movie\Director
    Debug Movie\DVD
    Debug Movie\Genre
    Debug Movie\imdbID
    Debug Movie\imdbRating
    Debug Movie\imdbVotes
    Debug Movie\Language
    Debug Movie\Metascore
    Debug Movie\Plot
    Debug Movie\Poster
    Debug Movie\Production
    Debug Movie\Ratings()\Source
    Debug Movie\Ratings()\Value
    Debug Movie\Released
    Debug Movie\Response
    Debug Movie\Runtime
    Debug Movie\Title
    Debug Movie\Type
    Debug Movie\Website
    Debug Movie\Writer
    Debug Movie\Year
  Else
    Debug "Error : " + Movie\Error
  EndIf
Else
  Debug "Didn't receive anything from the internet"
EndIf

End
Amateur Radio, D-STAR/VK3HAF
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: Help with extracting JSON data from file

Post by Bisonte »

Ok I found the mistake....

In your request is a space !
So we have to change the "Get_OMDB_Result_From_Url(Url.s, *Result.omdb_result)" procedure a little.

Change

Code: Select all

*Mem = ReceiveHTTPMemory(Url, #PB_HTTP_NoRedirect)
to

Code: Select all

*Mem = ReceiveHTTPMemory(URLEncoder(Url), #PB_HTTP_NoRedirect)
and the data will flow ;)

Edit : hmpf and now I have an API Key :shock: It's like TMDb ... for free...
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Help with extracting JSON data from file

Post by Mijikai »

Or use the demo key: BanMePlz :lol:
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Help with extracting JSON data from file

Post by Fangbeast »

Code: Select all

*Mem = ReceiveHTTPMemory(Url, #PB_HTTP_NoRedirect)
to
*Mem = ReceiveHTTPMemory(URLEncoder(Url), #PB_HTTP_NoRedirect)
and the data will flow ;)
Thanks for that, works. Just about to go over the road with my wife (To the bus stop).
Edit : hmpf and now I have an API Key :shock: It's like TMDb ... for free.
And I even found code I did in 2013 when I was less old and ugly. 2013!! Wow, an age ago when the body wasn't this hurt and the eyes were better (Sigh)
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Help with extracting JSON data from file

Post by Fangbeast »

Mijikai wrote:Or use the demo key: BanMePlz :lol:
Damn, I like your key better than my key (pant, pant, dribble)
Amateur Radio, D-STAR/VK3HAF
Post Reply