Recent

Author Topic: json from a json-newbie  (Read 364 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1308
json from a json-newbie
« on: May 03, 2025, 01:30:40 pm »
I am very new in json.

There is a working function looking similar to
Code: Pascal  [Select][+][-]
  1. function TAktienkurse_holen.vonPrep_HistoryAlsJson(Ticker: string; abDatum:TDate=0): TJSONData;  
  2.  
  3. const
  4. * my keys *
  5.  
  6. var HttpClient: TFPHTTPClient;  // Europäische Daten gibt es nur mit "ultimo"
  7.     JsonResponse: TJSONData;    // Abo und das kostet 99 im Monat
  8.     URL: string;
  9.     Response: string;
  10.     datum: TDate;
  11.  
  12. begin
  13.   result:=nil;
  14.   HttpClient := TFPHTTPClient.Create(nil);  // The program uses TFPHTTPClient to send a GET request
  15.   URL:= Base_Url +  'historical-price-full/' + Ticker + API_KEY;
  16.  
  17.     // die Umstellung der Zuweisungen führte dazu, dass es nicht mehr funktionierte
  18.   Response := HttpClient.Get(URL);    // Send GET request to fetch stock quote
  19.   JsonResponse := GetJSON(Response);  // The GetJSON function parses the JSON response.
  20.   Result := JsonResponse;  // diese Kombination funktioniert, die "Vereinfachung" nicht!
  21.   HttpClient.Free;
  22.  
  23. end;


This takes the data from one url and writes all of them to my database.
Unfortunately it only has US data.
If anybody needs it,
  BASE_URL = 'https://financialmodelingprep.com/api/v3/';
works fine.

Back to my topic.
I need European stocks as well and I asked Alpha Vantage for it. In "response" are exactly the data I want to have. Unfortunately the result of the function is empty. The data are lost in the line

Code: Pascal  [Select][+][-]
  1. JsonResponse := GetJSON(Response);  

As my whole logic is built on the work-on with json ad this code-interface and converting it to my own format and writing it in loops to my database, - I would prefer to fix this very line. Otherwise I can start again.

How do I write this very line differently?
Why does it work with the other service and not here?


TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: json from a json-newbie
« Reply #1 on: May 03, 2025, 01:46:01 pm »
How do I write this very line differently?
You don't. At least not without parsing the json yourself which is not advisable to do because that is the whole purpose of the getjson function.

Quote
Why does it work with the other service and not here?
You tell us :-)

Write out the response string in order to see what the actual returned string contains. That way it is possible to check if a valid json result was returned.

Also the error handling could be improved. Use a try finally block after creation of httpclient so that every http related action can be 'checked'.
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018