0

API calls in Ninox

Hi there all.

I have been spending a few days TRYING to get my head around API calls in Ninox. I can get a result if I hard code in the url and parameters, but am unsure how to structure the query using variables. For example: I am trying to get weather information. If I hard code "London" into the query, I get the results back for London. But if I get the user to enter their location into a box and then try use that I can't.

Now, when I get my data back, I can pull the entire response into a text box in JSON format. How do I then access the individual fields please? I have been searching all over and cannot find the answer.This is my code that I currently have in a button:  

let response := http("GET", "https://api.weatherapi.com/v1/current.json?q=London&key=77037*******************101");

'Json Response' := formatJSON(response.result);

 

I'd like to be able to put, for example: let location:=London, then put location into the query.

And the response: {"location":{"name":"London","region":"City of London, Greater London","country":"United Kingdom","lat":51.52,"lon":-0.11,"tz_id":"Europe/London","localtime_epoch":1706513221,"localtime":"2024-01-29 7:27"},"current":{"temp_c":9,"temp_f":48.2,"is_day":0,"condition":{"text":"Fog","icon":"//cdn.weatherapi.com/weather/64x64/night/248.png","code":1135},"wind_mph":10.5,"wind_degree":170,"wind_dir":"S","pressure_mb":1023,"precip_mm":0,"humidity":93,"cloud":0,"feelslike_c":8.2,"vis_miles":0,"uv":1,"gust_mph":15}}

I hope my 2 questions make sense. I don't just want the code, but an explanation of what to do would be awesome.

 

Thanks for your time and I hope my ramblings make sense to you clever people out there.

4 replies

null
    • Ninox partner
    • RoSoft_Steven.1
    • 2 mths ago
    • Reported - view

    Hi ,

    You could make the URL in front with a variable like:

    let URL := "https://api.weatherapi.com/v1/current.json?q="+datatextfieldContainingThePlace+"&key=77037*******************101"
    let response := http("GET", URL);
    
    'Json Response' := formatJSON(response.result);
    

    for the response you can use the variable 'Json Response' like you would use a select() function like:

    'Json Response'.location.name <gives you the Location Name

    'Json Response'.current.temp_c     < gives you the temperature

    'Json Response'.current.condition.text   <gives you the weathercondition

      • John_Wilmans
      • 2 mths ago
      • Reported - view

       Thanks for the help! I have figured out the building of the URL with your help. Thank you!

       

      However, I've tried a number of ways of getting the response out in a usuable format and keep getting an error. I am sending it to an alert and keep getting it back blank. If I put in 'Json Response' := formatJSON(reSponse.result), I get ALL the results. But if I try get a single field it comes back blank. I'm guessing its a formatting error somewhere?

       

      Thanks for the help so far.

      • John_Wilmans
      • 2 mths ago
      • Reported - view

       Sorry. My fault. IT'S WORKING!!!

      • Ninox partner
      • RoSoft_Steven.1
      • 2 mths ago
      • Reported - view

       👍 You're welcome. I'm glad it works.

Content aside

  • Status Answered
  • 2 mths agoLast active
  • 4Replies
  • 69Views
  • 2 Following