The big JSON-file giving the collection of Netatmo-station data is output of a PHP-script,
using original Netatmo API-call to get Public data available from the stations within a designated, geo-oriented search-window.
The idea is to have that big JSON-file as data-source for extraction for several selected stations, with separate extracting-script per selected station to extract the detailled data aimed/tuned at an application.
The single PHP-script periodically 'pulls' data from a remote server at internet, while the extracting-scripts as satellites locally perform their functions at 'convenient times', not loading the external interfaces.
Because of this 'demarcation', those extracting-scripts
may be in any other language than PHP.
Myself not well-versed in PHP, but having the subsequent user-applications in Python,
is one reason to try Python for the interfacing software.
Meanwhile have pensively been triggered for another aspect of your test_list in which all textual elements enclosed in " ",
because of a statement found at
W3Schools
In JSON, string values must be written with double quotes:
JSON
{"name":"John"}
That is applicable in the original JSON-file, but not in the extracted text-file Station_segment:
Is easy to remedy by scriptline
Code: Select all
Station_segment = Station_segment.replace("'", '"')
Although the resulting stringprint looks very similar to your test_list, still the same error report
- Spoiler: show
- Station_segment_string3 =
'[ { "land": "DE", "stad": "Borkum", "straat": null, "id": "70:ee:50:36:f3:40", "lat": 53.5905, "lon": 6.6623173, "tijdzone": "Europe/Berlin", "tijd": "2022-11-24 23:42:49", "measures": {"temperature": 7.1, "humidity": 96, "pressure": 1023.8, "rain_60min": 0, "rain_24h": 0.404, "rain_live": 0, "rain_timeutc": "2022-11-24 23:42:28", "wind_strength": 3, "wind_angle": -1, "gust_strength": 12, "gust_angle": 261, "wind_timeutc": "2022-11-24 23:42:47"} } ]'
Traceback (most recent call last):
File "/home/pi/Python_scripts/JSON_NetatmoPublic_to_HWA03_xxxx_0198.py", line 220, in <module>
test_list_dict = json.loads(test_list)
File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Mimick for subsitute of your test_file has been done, and strange thing is that your testscriptlines
completely work if Station_segment_string3 is
manually copied from Putty's CLI as substitute for the string in test_list in your proposed testscriptlines.
Next experiments are very rude try&error
It
partlially works when I insert a scriptline with json.dumps() to catch the text-string Station_segment as input for making test_list
Code: Select all
test_list = json.dumps(Station_segment)
test_list_dict = json.loads(test_list)
print('Dictionary =')
print(test_list_dict)
# Find dictionary matching value in list
res = None
for sub in test_list_dict:
if sub["id"] == "70:ee:50:36:f3:40":
res = sub
break
# printing result
print("The filtered station data is : " + str(res))
print("The temperature : " + str(res['measures']['temperature']))
print
=> getting a dictionary, but stuck at subsequent finding of sub["id"]
- Spoiler: show
- Station_segment_string3 = change of head & tail
'[ { "land": "DE", "stad": "Borkum", "straat": null, "id": "70:ee:50:36:f3:40", "lat": 53.5905, "lon": 6.6623173, "tijdzone": "Europe/Berlin", "tijd": "2022-11-25 08:53:15", "measures": {"temperature": 8.1, "humidity": 97, "pressure": 1024.8, "rain_60min": 0.101, "rain_24h": 2.121, "rain_live": 0, "rain_timeutc": "2022-11-25 08:53:12", "wind_strength": 3, "wind_angle": -1, "gust_strength": 5, "gust_angle": 359, "wind_timeutc": "2022-11-25 08:53:12"} } ]'
Dictionary =
'[ { "land": "DE", "stad": "Borkum", "straat": null, "id": "70:ee:50:36:f3:40", "lat": 53.5905, "lon": 6.6623173, "tijdzone": "Europe/Berlin", "tijd": "2022-11-25 08:53:15", "measures": {"temperature": 8.1, "humidity": 97, "pressure": 1024.8, "rain_60min": 0.101, "rain_24h": 2.121, "rain_live": 0, "rain_timeutc": "2022-11-25 08:53:12", "wind_strength": 3, "wind_angle": -1, "gust_strength": 5, "gust_angle": 359, "wind_timeutc": "2022-11-25 08:53:12"} } ]'
Traceback (most recent call last):
File "/home/pi/Python_scripts/JSON_NetatmoPublic_to_HWA03_xxxx_0198.py", line 230, in <module>
if sub["id"] == "70:ee:50:36:f3:40":
TypeError: string indices must be integers
Trying direct reading from that dictionary with scriptline
Code: Select all
test_extract = test_list_dict["land"]
=> no luck, with same kind of error report
- Spoiler: show
- Dictionary =
'[ { "land": "DE", "stad": "Borkum", "straat": null, "id": "70:ee:50:36:f3:40", "lat": 53.5905, "lon": 6.6623173, "tijdzone": "Europe/Berlin", "tijd": "2022-11-25 09:23:30", "measures": {"temperature": 8.1, "humidity": 97, "pressure": 1025.2, "rain_60min": 0, "rain_24h": 2.121, "rain_live": 0, "rain_timeutc": "2022-11-25 09:23:20", "wind_strength": 3, "wind_angle": 11, "gust_strength": 5, "gust_angle": 216, "wind_timeutc": "2022-11-25 09:23:26"} } ]'
Traceback (most recent call last):
File "/home/pi/Python_scripts/JSON_NetatmoPublic_to_HWA03_xxxx_0198.py", line 228, in <module>
test_extract = test_list_dict["land"]
TypeError: string indices must be integers
We seem to be missing something subtle in filehandling in Python3.x & JSON .......