[SOLVED] Access violation in JSON data lazarus / 21-General ---------------------------------------------------------------------- maurobio · 2021-09-16 20:01 Dear ALL, Could someone explain to me why the piece of code below fail with a dreadful 'Access violation' message? The JSON data it gets is valid (see attached zip file). 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 test_gbif; {$mode objfpc}{$H+} uses SysUtils, fphttpclient, fpjson, jsonparser; const         url = 'https://api.gbif.org/v1/species?name=';        searchStr = 'Myrcia lingua'; var  jData, jItem, jItems: TJSONData;  scientificname, status, valid_name, taxon_list: string;  begin  jData := GetJSON(TFPHTTPClient.SimpleGet(url + StringReplace(searchStr, ' ', '%20', [rfReplaceAll])));  jItems := jData.FindPath('results');  jItem := jItems.Items[0]; // Just want the first result  scientificname := jItem.FindPath('scientificname').AsString;  status := jItem.FindPath('taxonomicStatus').AsString;  valid_name := jItem.FindPath('accepted').AsString;  taxon_list := jItem.FindPath('kingdom').AsString + '; ' +        jItem.FindPath('phylum').AsString + '; ' +        jItem.FindPath('class').AsString + '; ' +        jItem.FindPath('order').AsString + '; ' +        jItem.FindPath('family').AsString;  jData.Free;end.  Thanks in advance for any assistance you can provide. With best wishes, ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-09-16 20:20 Boas Maurobio, I don't know if you're aware, but if FindPath doesn't find the path you asked for, it will return a nil. Instead of blindly just assume that all the paths that you are seeking exist, you should first test if the result of FindPath is not nil. I haven't tested with the specific data you presented, but I'm assuming that the Access Violation error is because you're trying to do something with a variable that contains nil. Hope this helps!! Grande abraço, Gus ---------------------------------------------------------------------- maurobio · 2021-09-16 20:33 @Gus, Thanks for your answer. Sure, I had presumed that the access violation is because a nil value is being returned - but why, that's my question! The JSON data returned is perfectly valid (and in fact relatively simple). With best wishes, ---------------------------------------------------------------------- maurobio · 2021-09-16 20:47 Hi, Just to present a possible solution... The piece of code below, which uses the JsonTools library (https://www.getlazarus.org/json/) works fine: 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 gbif_example; uses SysUtils, fphttpclient, JsonTools; const         url = 'https://api.gbif.org/v1/species?name=';        searchName = 'Myrcia lingua'; var  rawJson: AnsiString;  JsonData: TJsonNode;  scientificname, status, valid_name, taxon_list: string; begin  WriteLn('Searching for: ' + searchName);  // Get the JSON data  JsonData := TJsonNode.Create;   rawJson := TFPHTTPClient.SimpleGet(url + StringReplace(searchName, ' ', '%20', [rfReplaceAll]));  JsonData.Parse(rawJson);  scientificname := JsonData.Find('results/0/scientificName').AsString;  status := JsonData.Find('results/0/taxonomicStatus').AsString;  valid_name := JsonData.Find('results/0/accepted').AsString;  taxon_list := JsonData.Find('results/0/kingdom').AsString + '; ' +        JsonData.Find('results/0/phylum').AsString + '; ' +        JsonData.Find('results/0/class').AsString + '; ' +        JsonData.Find('results/0/order').AsString + '; ' +        JsonData.Find('results/0/family').AsString;       JsonData.Free;  WriteLn(scientificname);  WriteLn(status);  WriteLn(valid_name);  WriteLn(taxon_list);end. In general, I have been using JsonTools instead of the JSON parsing libraries included with FPC/Lazarus. According to comparisons presented by its author (https://www.getlazarus.org/json/tests/), that library is even more efficient. With best wishes, ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-09-16 20:47 Boas Maurobio, I had a look at the JSON returned from the link in your code and I think I know the issue: The member in the returned JSON is scientificName but you're asking for scientificname. I might be wrong, cuz I haven't confirmed it with code, but if I'm not mistaken, JSON is case sensitive, well, at least the fpjson implementation. So if you make sure that all the members are correctly capitalized, I'm guessing that your code will run without a hitch In order to minimize these issues and to make simple changes easier, I tend to have all the member names in constants: 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 const  cJSONScientificName = 'scientificName'; scientificname := jItem.FindPath(cJSONScientificName).AsString; This way if I have to use the same member more than once, I don't have to make sure if I made a typo, cuz I'll use the auto-completion of Lazarus to complete the constant name. Grande abraço, Gus ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-09-16 20:52 Boas Maurobio, Quote from: maurobio on September 16, 2021, 10:47:10 pmCode: 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 gbif_example;  scientificname := JsonData.Find('results/0/scientificName').AsString;end. You've just confirmed my suspicions of the case sensitive nature of JSON!!! If you look at the piece of code above, you have the correct capitalization of scientificName, but you have the wrong one on the piece of code on your first post. Try to use string constants for the members, instead of string literals. For me the ability to use the auto-complete on Lazarus is really worth it for these small bugs!! Hope this solves your problem!! Grande abraço, Gus ---------------------------------------------------------------------- Bart · 2021-09-16 21:02 After fixing the casing in scientificName I get: valid_name=Myrcia guianensis (Aubl.) DC. taxon_list=Plantae; Tracheophyta; Magnoliopsida; Myrtales; Myrtaceae Hope this is what you expected. Bart ---------------------------------------------------------------------- maurobio · 2021-09-16 21:25 @Gus, @Bart, That is it! I did not pay attention to the fact that JSON is, in fact, case-sensitive. Thank you very much for your time and patience! With best wishes, ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-09-16 21:31 Boas Maurobio, Quote from: maurobio on September 16, 2021, 11:25:37 pmThank you very much for your time and patience! You're more than welcome!! Grande abraço, Gus