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?
Fail to parse a json in api.apixu.com
Re: Fail to parse a json in api.apixu.com
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.
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.
Re: Fail to parse a json in api.apixu.com
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
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
Re: Fail to parse a json in api.apixu.com
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...
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...
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...
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
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}}
>>>
Also worth telling people in what way you are running it. We don't know what platform, version, how you got urequests...
Re: Fail to parse a json in api.apixu.com
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.
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.