url json data question lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Aruna · 2022-12-20 01:39 I am experimenting with JSON out there on the public domain internet and my code works ( sort of ). I have a Tbutton which when clicked goes and grabs the JSON data from the openweathermap.org weather API and displays this in the Tmemo. This part works. As can be seen below. I was checking the current weather in Mongolia. Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {"coord":{" lon":106.8832,"lat":47.9077},"weather":[{"id":802,"main":"Clouds","des cription":"scattered clouds","icon":"03d"}],"base":"stations","main":{ "temp":-25.93,"feels_like":-32.93,"temp_min":-25.93,"temp_max":- 25.93,"pressure":1016,"humidity":76},"visibility":10000,"wind":{"speed ":4,"deg":170},"clouds":{"all":40},"dt":1671499205,"sys":{"type":1,"id ":9644,"country":"MN","sunrise":1671496702,"sunset":1671526878},"timez one":28800,"id":2028462,"name":"Ulaanbaatar","cod":200} Now, How do I pick out a specific name: value(s) pair in that jason response? My code is below. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,  opensslsockets,fphttpclient,fpjson, jsonparser; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    jsonData: TMemo;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1;  jObj: TJSONObject;  jArr: TJSONArray;  jEnum:TJSONEnum;  response: string; implementation {$R *.lfm}  { TForm1 } procedure TForm1.Button1Click(Sender: TObject);   begin  jArr:=TJSONArray.Create;   try     response:=TFPHTTPClient.SimpleGet(' https://api.openweathermap.org/data/2.5/weather?q=Ulaanbaatar&appid=a0 a47bddbbfe8e4c2905cf76f64b13ef&units=metric');     writeln(response);     jsonData.Lines.Text:=response;  finally     jArr.Free;  end;  end;end.  And this is from a youtube video I stumbled upon by accident Learn how to consume JSON data in FreePascal which is from  Aug 23, 2020 is there a better way to do things now or this is just fine? Thanks, Aruna PS: That is my active working API-Key so please use in moderation thank you. ---------------------------------------------------------------------- paweld · 2022-12-20 06:12 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  fphttpclient, opensslsockets, fpjson, jsonparser; procedure TForm1.Button1Click(Sender: TObject);var  json: String;  jd: TJSONData;begin  json := TFPHTTPClient.SimpleGet('https://api.openweat hermap.org/data/2.5/weather?q=Ulaanbaatar&appid=a0a47bddbbfe8e4c2905cf 76f64b13ef&units=metric');  jd := GetJSON(json);  Memo1.Lines.Text := jd.FormatJSON([], 2);  jd.Freeend; procedure TForm1.Button2Click(Sender: TObject);var  jd, jdweather: TJSONData;  i: Integer;begin  Memo2.Lines.Clear;  jd := GetJSON(Memo1.Lines.Text);  if jd.FindPath('.name') <> nil then    Memo2.Lines.Add('City: ' + jd.GetPath('.name').AsString);  if (jd.FindPath('.weather') <> nil) and (jd.GetPath('.weather').Count > 0) then  begin    jdweather := jd.GetPath('.weather');    Memo2.Lines.Add('Description: ');    for i := 0 to jdweather.Count - 1 do    begin      if jdweather.Items[i].FindPath('.description') <> nil then        Memo2.Lines.Add(Format('        %2d - %s', [i, jdweather.Items[i].GetPath('.description').AsString]));    end;  end;  if jd.FindPath('.main.temp') <> nil then    Memo2.Lines.Add('Temp: ' + FormatFloat('0.00', jd.GetPath('.main.temp').AsFloat));  if jd.FindPath('.main.humidity') <> nil then    Memo2.Lines.Add(Format('Humidity: %3.1f%%', [jd.GetPath('.main.humidity').AsFloat]));  jd.Freeend; ---------------------------------------------------------------------- PierceNg · 2022-12-20 06:12 Quote from: Aruna on December 20, 2022, 02:39:18 amNow, How do I pick out a specific name: value(s) pair in that jason response? And this is from a youtube video I stumbled upon by accident Learn how to consume JSON data in FreePascal which is from  Aug 23, 2020 is there a better way to do things now or this is just fine? Below code produces following output: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i % fpc owpjson.lpr...29 lines compiled, 0.2 sec % ./owpjson      According to Open Weather Maps: scattered clouds in Ulaanbaatar Heap dump by heaptrc unit of /home/pierce/pascal/work/owp/owpjson228 memory blocks allocated : 12049/12576228 memory blocks freed     : 12049/125760 unfreed memory blocks : 0True heap size : 360448True free heap : 360448 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program owpjson; {$mode objfpc}{$H+} uses  heaptrc, fpjson, jsonparser; const  OWP_RESP = '{"coord":{"lon":106.8832,"lat":47.9077},"weather":[{"id":8 02,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"ba se":"stations","main":{"temp":-25.93,"feels_like":-32.93,"temp_min":- 25.93,"temp_max":- 25.93,"pressure":1016,"humidity":76},"visibility":10000,"wind":{"speed ":4,"deg":170},"clouds":{"all":40},"dt":1671499205,"sys":{"type":1,"id ":9644,"country":"MN","sunrise":1671496702,"sunset":1671526878},"timez one":28800,"id":2028462,"name":"Ulaanbaatar","cod":200}'; var  jo1, jo2: TJSONObject;  ja: TJSONArray; begin  jo1 := GetJSON(OWP_RESP) as TJSONObject;  try    if jo1.Find('weather', ja) then      begin      jo2 := ja.Extract(0) as TJSONObject;        writeln('According to OpenWeatherMaps: ', jo2['description'].AsString, ' in ', jo1['name'].AsString);        writeln;      end;  finally    jo1.Free;    jo2.Free;    //ja.Free; // <=== This line causes access violation.  end;end. The heap dump part is to show no leak. Didn't watch the video you linked. ---------------------------------------------------------------------- Aruna · 2022-12-21 02:01 Quote from: paweld on December 20, 2022, 07:12:24 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  fphttpclient, opensslsockets, fpjson, jsonparser; procedure TForm1.Button1Click(Sender: TObject);var  json: String;  jd: TJSONData;begin  json := TFPHTTPClient.SimpleGet('https://api.openweat hermap.org/data/2.5/weather?q=Ulaanbaatar&appid=a0a47bddbbfe8e4c2905cf 76f64b13ef&units=metric');  jd := GetJSON(json);  Memo1.Lines.Text := jd.FormatJSON([], 2);  jd.Freeend; procedure TForm1.Button2Click(Sender: TObject);var  jd, jdweather: TJSONData;  i: Integer;begin  Memo2.Lines.Clear;  jd := GetJSON(Memo1.Lines.Text);  if jd.FindPath('.name') <> nil then    Memo2.Lines.Add('City: ' + jd.GetPath('.name').AsString);  if (jd.FindPath('.weather') <> nil) and (jd.GetPath('.weather').Count > 0) then  begin    jdweather := jd.GetPath('.weather');    Memo2.Lines.Add('Description: ');    for i := 0 to jdweather.Count - 1 do    begin      if jdweather.Items[i].FindPath('.description') <> nil then        Memo2.Lines.Add(Format('        %2d - %s', [i, jdweather.Items[i].GetPath('.description').AsString]));    end;  end;  if jd.FindPath('.main.temp') <> nil then    Memo2.Lines.Add('Temp: ' + FormatFloat('0.00', jd.GetPath('.main.temp').AsFloat));  if jd.FindPath('.main.humidity') <> nil then    Memo2.Lines.Add(Format('Humidity: %3.1f%%', [jd.GetPath('.main.humidity').AsFloat]));  jd.Freeend; Thank you so much @paweld. As you can see from the attached screenshot with your help things are working. Now I have to go through your code and try to understand why it works. Once again very much appreciate you taking the time. I suppose I should not ask for a heavily commented version? :-D Note to self: Why does no one comment anything anymore.. ---------------------------------------------------------------------- Aruna · 2022-12-21 02:10 Quote from: PierceNg on December 20, 2022, 07:12:43 am Below code produces following output: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i % fpc owpjson.lpr...29 lines compiled, 0.2 sec % ./owpjson      According to Open Weather Maps: scattered clouds in Ulaanbaatar Heap dump by heaptrc unit of /home/pierce/pascal/work/owp/owpjson228 memory blocks allocated : 12049/12576228 memory blocks freed     : 12049/125760 unfreed memory blocks : 0True heap size : 360448True free heap : 360448 The heap dump part is to show no leak. Didn't watch the video you linked. This was a very pleasant surprise I had no idea it was this simple to do this and check for leaks, amazing! I will test your code then get back to you. Many thanks for taking your valuable time to help me.