Recent

Author Topic: How to retrieve Data from JSON file (Web Request)?  (Read 2650 times)

MrMaxMusterman

  • Guest
How to retrieve Data from JSON file (Web Request)?
« on: May 21, 2021, 11:46:38 pm »
I have a question concerning retrieving data from a JSON file with i get by a URL request.
I already have a few code snippets together from other forum articles (see below)


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Http:TFPHttpClient;
  4.   Content:String;
  5.   Json:TJSONData;
  6.   Lat:string;
  7. begin
  8.    Http:=TFPHttpClient.Create(Nil);
  9.    try
  10.      Content:=Http.Get('https://www.mywebsite.com');
  11.      JSON:=GetJSON(Content);
  12.      try
  13.        //Retrieving Data from JSON
  14.      finally
  15.      end;
  16.    finally
  17.    end;
  18. end;

My question now becomes how i'm now able to retrieve data from the JSON File. Down below, i have the whole JSON File which is displayed when requesting the desired URL

Code: Pascal  [Select][+][-]
  1. {
  2.    "results" : [
  3.       {
  4.          "address_components" : [
  5.             {
  6.                "long_name" : "20",
  7.                "short_name" : "20",
  8.                "types" : [ "street_number" ]
  9.             },
  10.             {
  11.                "long_name" : "West 34th Street",
  12.                "short_name" : "W 34th St",
  13.                "types" : [ "route" ]
  14.             },
  15.             {
  16.                "long_name" : "Manhattan",
  17.                "short_name" : "Manhattan",
  18.                "types" : [ "political", "sublocality", "sublocality_level_1" ]
  19.             },
  20.             {
  21.                "long_name" : "New York",
  22.                "short_name" : "New York",
  23.                "types" : [ "locality", "political" ]
  24.             },
  25.             {
  26.                "long_name" : "New York County",
  27.                "short_name" : "New York County",
  28.                "types" : [ "administrative_area_level_2", "political" ]
  29.             },
  30.             {
  31.                "long_name" : "New York",
  32.                "short_name" : "NY",
  33.                "types" : [ "administrative_area_level_1", "political" ]
  34.             },
  35.             {
  36.                "long_name" : "USA",
  37.                "short_name" : "US",
  38.                "types" : [ "country", "political" ]
  39.             },
  40.             {
  41.                "long_name" : "10118",
  42.                "short_name" : "10118",
  43.                "types" : [ "postal_code" ]
  44.             }
  45.          ],
  46.          "formatted_address" : "20 W 34th St, New York, NY 10118, USA",
  47.          "geometry" : {
  48.             "location" : {
  49.                "lat" : 40.748558,
  50.                "lng" : -73.9857578
  51.             },
  52.             "location_type" : "ROOFTOP",
  53.             "viewport" : {
  54.                "northeast" : {
  55.                   "lat" : 40.74990698029149,
  56.                   "lng" : -73.98440881970849
  57.                },
  58.                "southwest" : {
  59.                   "lat" : 40.74720901970849,
  60.                   "lng" : -73.98710678029151
  61.                }
  62.             }
  63.          },
  64.          "place_id" : "ChIJr92XCKlZwokRIy2j_7Rb2V8",
  65.          "plus_code" : {
  66.             "compound_code" : "P2X7+CM New York City, New York, USA",
  67.             "global_code" : "87G8P2X7+CM"
  68.          },
  69.          "types" : [ "street_address" ]
  70.       }
  71.    ],
  72.    "status" : "OK"
  73. }

In my case, I want to retrieve the "lat" and "lng" values out of the JSON File.

I would really appreciate your help with this. Thanks in advance.

zamronypj

  • Full Member
  • ***
  • Posts: 133
    • Fano Framework, Free Pascal web application framework
Re: How to retrieve Data from JSON file (Web Request)?
« Reply #1 on: May 22, 2021, 01:43:20 am »
https://wiki.freepascal.org/fcl-json

Code: [Select]
lat := json.FindPath('results[0].geometry.location.lat').AsFloat;
lon := json.FindPath('results[0].geometry.location.lng').AsFloat;
« Last Edit: May 22, 2021, 01:45:51 am by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to retrieve Data from JSON file (Web Request)?
« Reply #2 on: May 22, 2021, 02:02:43 am »
Or:
Code: Pascal  [Select][+][-]
  1. var
  2.   location:TJSONObject;
  3.   lat,lng:Single;
  4. ...
  5.   location:=TJSONObject(Json.FindPath('results[0].geometry.location'));
  6.   lat:=location.Floats['lat'];
  7.   lng:=location.Floats['lng'];
  8.  

MrMaxMusterman

  • Guest
Re: How to retrieve Data from JSON file (Web Request)?
« Reply #3 on: May 22, 2021, 10:47:54 am »
First off, thanks for your help.

I tried implementing all this but sadly, i always get EHTTPClient Exeption Class due to Status Code 400.

Idk why but just to make things clear, its an https website with a JSON File being hosted on.
Did I maybe forget something under the "uses" category or so?

Here is my full source code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fphttpclient, fpjson, jsonparser, opensslsockets;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. var
  37.   Client:TFPHttpClient;
  38.   Content:String;
  39.   Json:TJSONData;
  40.   Lat,Lng:string;
  41.   Location:string;
  42. begin
  43.    Client:=TFPHttpClient.Create(Nil);
  44.    Client.AllowRedirect:=true;
  45.    Location:=Edit1.Text;
  46.    try
  47.      Content:=Client.Get('https://mywebsite'+Location+'APIKEY');
  48.      JSON:=GetJSON(Content);
  49.      try
  50.        Lat:=json.FindPath('results[0].geometry.location.lat').AsString;
  51.        Lng:=json.FindPath('results[0].geometry.location.lng').AsString;
  52.      finally
  53.      end;
  54.    finally
  55.    end;
  56.    Label1.Caption:=Lat;
  57.    Label2.Caption:=Lng;
  58. end;
  59.  
  60. end.
  61.  

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to retrieve Data from JSON file (Web Request)?
« Reply #4 on: May 22, 2021, 02:37:52 pm »
I don't see a missing unit.

HTTP code 400 means the server is not happy with your request. Something is wrong with the request itself. Sometimes the server sends a message to explain more.

Put the request you are sending here. Obsecure any sensitive data with ####

Edit:
Probably it wants a User Agent. Try adding:
Code: Pascal  [Select][+][-]
  1.   Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  2.   Client.AddHeader('Content-Type', 'application/json');

right after Client.AllowRedirect
« Last Edit: May 22, 2021, 02:47:22 pm by engkin »

MrMaxMusterman

  • Guest
Re: How to retrieve Data from JSON file (Web Request)?
« Reply #5 on: May 22, 2021, 03:17:07 pm »
Sadly, your idea with adding a user agent etc. didnt work out. Here is my full source code with the actual request:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fphttpclient, fpjson, jsonparser, opensslsockets;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     Edit1: TEdit;
  18.     Label1: TLabel;
  19.     Label2: TLabel;
  20.     procedure Button1Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);
  37. var
  38.   Client:TFPHttpClient;
  39.   Content:String;
  40.   Json:TJSONData;
  41.   Lat,Lng:Single;
  42.   Location:string;
  43. begin
  44.    Client:=TFPHttpClient.Create(Nil);
  45.    Client.AllowRedirect:=true;
  46.    Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  47.    Client.AddHeader('Content-Type', 'application/json');
  48.    Location:=Edit1.Text;
  49.    try
  50.      Content:=Client.Get('https://maps.googleapis.com/maps/api/geocode/json?address= '+Location+' &key={MyAPIKey}');
  51.      JSON:=GetJSON(Content);
  52.      try
  53.        Lat:=json.FindPath('results[0].geometry.location.lat').AsFloat;
  54.        Lng:=json.FindPath('results[0].geometry.location.lng').AsFloat;
  55.      finally
  56.      end;
  57.    finally
  58.    end;
  59.    Label1.Caption:=FloatToStr(Lat);
  60.    Label2.Caption:=FloatToStr(Lng);
  61. end;
  62. end.
  63.  

The only thing i put out of this source code is my api key from google. It isnt the problem though cause if im actually using the link (so my request including an adress, which can be formatted in any way possible) in my browser, ill get the full json file.

Can you mabye tell me how i can gain more info on the error message itself? I only get "Project project1 got Exeption Class EHTTPPClient with following message: Unexpected response status code:400" (translated)
« Last Edit: May 22, 2021, 03:28:40 pm by MrMaxMusterman »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to retrieve Data from JSON file (Web Request)?
« Reply #6 on: May 22, 2021, 03:52:51 pm »
Since it works in your browser, get the request your browser is sending. Search for an Add On, Extension, or Plug In for your browser that would let you see the request including the headers. Then simply replicate it.

Search for "HTTP 400":
Here is one result


Edit:
I bet there is a cookie in your browser that needs to be sent as well. You had the authorize your browser at somepoint.
« Last Edit: May 22, 2021, 04:02:44 pm by engkin »

MrMaxMusterman

  • Guest
Re: How to retrieve Data from JSON file (Web Request)?
« Reply #7 on: May 22, 2021, 04:08:58 pm »
I actually found out why the program wasnt working the whole time...

The problem was that for some reason, Lazarus didnt convert spaces between each Adress Info (e.g. Street Name and Street Number) that the user would type into the edit box into a new request with no spaces whatsoever.

Is there maybe a way to replace all empty spaces in a string (userinput) with something else (e.g. commas)?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to retrieve Data from JSON file (Web Request)?
« Reply #8 on: May 22, 2021, 04:37:24 pm »
Try using EncodeURLElement?

It turns
123 Street, City, State 12345
into
123%20Street,%20City,%20State%2012345

MrMaxMusterman

  • Guest
Re: How to retrieve Data from JSON file (Web Request)?
« Reply #9 on: May 22, 2021, 06:24:06 pm »
Thank you so much!!

Now everything works as intended.

 

TinyPortal © 2005-2018