Fail to parse a json in api.apixu.com

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
bergpb
Posts: 6
Joined: Sun Jun 10, 2018 2:51 pm

Fail to parse a json in api.apixu.com

Post by bergpb » Mon Jun 11, 2018 12:28 am

Hello guys,
Im try to parse a json when a i get from Apixu, but i get this error:

This is my code:
import urequests
res = urequests.get('https://api.apixu.com/v1/current.json?k ... =Fortaleza')
forecast = res_temp.json()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "urequests.py", line 32, in json
ValueError: syntax error in JSON

Can someone help me to get this json?

cefn
Posts: 205
Joined: Tue Aug 09, 2016 10:58 am

Re: Fail to parse a json in api.apixu.com

Post by cefn » Wed Jun 13, 2018 9:23 pm

I think it would be useful to see a copy of the JSON payload. No idea how to approach solving the problem given the reference URL you have provided doesn't work since the API key has been removed.

What is the value of the res.content property? There's a chance that it isn't JSON at all.

If it IS json, but badly formed, you might find that approaching the JSON in a more token-oriented way could help https://github.com/ShrimpingIt/medea

Using a tokenizer rather than .json might enable you to to recover from syntax errors and continue to tokenize valid sub-trees even if the document as a whole is invalid JSON.

However, it's probably overkill if you know what you need from the document and can simply search for it in the res.content string directly.

bergpb
Posts: 6
Joined: Sun Jun 10, 2018 2:51 pm

Re: Fail to parse a json in api.apixu.com

Post by bergpb » Fri Jun 15, 2018 1:42 pm

In res content return a response, json is okay in formating.
But in res.json get a error.

Here a url with api key, can you try:
https://api.apixu.com/v1/current.json?k ... =Fortaleza

cefn
Posts: 205
Joined: Tue Aug 09, 2016 10:58 am

Re: Fail to parse a json in api.apixu.com

Post by cefn » Fri Jun 15, 2018 2:36 pm

Worth creating an actual test case which runs at least before asking people to fix your code. You didn't prove this in your original code. Running it reveals...

Code: Select all

cefn-bionic-thinkpad:unix$ micropython 
MicroPython v1.9.4-dirty on 2018-06-15; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import urequests
=== res = urequests.get('https://api.apixu.com/v1/current.json?key=myapikey&q=Fortaleza')
=== forecast = res_temp.json()
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
NameError: name 'res_temp' is not defined
After fixing your code to no longer refer to a non-existent variable res_temp I get the following in unix micropython which suggests there's nothing fundamentally wrong at least when connecting from here...

Code: Select all

cefn-bionic-thinkpad:unix$ micropython 
MicroPython v1.9.4-dirty on 2018-06-15; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import urequests
=== res = urequests.get('https://api.apixu.com/v1/current.json?key=5cb0a473942c49da9ec194251181006&q=Fortaleza')
=== forecast = res.json()
=== 
>>> forecast
{'location': {'lon': -41.42, 'tz_id': 'America/Fortaleza', 'name': 'Fortaleza', 'localtime_epoch': 1529072865, 'lat': -3.32, 'region': 'Ceara', 'country': 'Brazil', 'localtime': '2018-06-15 11:27'}, 'current': {'vis_miles': 12.0, 'wind_mph': 8.1, 'humidity': 77, 'precip_mm': 0.0, 'feelslike_c': 24.0, 'wind_degree': 111, 'temp_f': 73.40000000000001, 'wind_kph': 13.0, 'feelslike_f': 75.2, 'precip_in': 0.0, 'condition': {'text': 'Mist', 'code': 1030, 'icon': '//cdn.apixu.com/weather/64x64/day/143.png'}, 'wind_dir': 'ESE', 'temp_c': 23.0, 'pressure_in': 30.5, 'pressure_mb': 1016.0, 'is_day': 1, 'last_updated': '2018-06-15 11:12', 'vis_km': 20.0, 'last_updated_epoch': 1529071952, 'cloud': 10}}
>>> 
So we're back to the question already asked, what is the value of res.content? That will indicate what you are actually receiving.

Also worth telling people in what way you are running it. We don't know what platform, version, how you got urequests...

bergpb
Posts: 6
Joined: Sun Jun 10, 2018 2:51 pm

Re: Fail to parse a json in api.apixu.com

Post by bergpb » Fri Jun 15, 2018 2:54 pm

Sorry for my wrong code.
Im use esp8266(esp12 standalone) with latest micropython (esp8266-20180511-v1.9.4).
Weird i tested the code now and works.
Just on i print the res.json() my esp reboots. Looks like a memory allocation error.
Im tested your code above.

Post Reply