Document my insanity as I don't understand why json.loads() breaks.

master
josiah 4 years ago
parent b7b702baef
commit 71a2df01f8

@ -14,5 +14,64 @@ arke is a dumb python monitoring client i am currently working on to monitor htt
## TODO
- [ ] Fix loading json from file
- alright see "json loading" heading in this file.
- [ ] Fix state comparison and post to only post the item that has changed since last run
- [ ] update this readme.
## json loading
confused here. in python, a dict is the key/value store that is used to map to json. all my functions return a dict:
``` python
def monitor_HttpTargets(monitoringtargets):
responseTable = {}
...
return responseTable
def monitor_DomainExpiry(targets):
responseTable = {}
...
return responseTable
def monitor_TlsExpiry(targets):
responseTable = {}
...
return responseTable
```
Ok so that part's straight forward. Then, i combine all of them into a single keyed dict, so I have a dict of dicts:
``` python
datastore = {}
datastore['http'] = monitor_HttpTargets(arkevars.httpTargets)
datastore['certs'] = monitor_TlsExpiry(arkevars.tlsTargets)
datastore['whois'] = monitor_DomainExpiry(arkevars.domains_to_check)
```
OK again, straight forward. Here's where shit gets less obvious to me. I need to convert this variable containing a dict into a json object in a file. I do this like so:
```python
# write new results to file
with open(this_round_file, "a+", encoding="utf-8") as outfile:
json.dump(datastore, outfile, ensure_ascii=False, sort_keys=True)
```
ok, so crafting the json object seems to make sense, and seems to be pretty straight forward. What about reading the 'json' object? WELL THEREIN LIES THE TALE MY GOOD BITCH
```python
Traceback (most recent call last):
File "arke.py", line 114, in <module>
json_data = json.load(json_File)
File "/usr/lib/python3.8/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 133 (char 132)
```
this is where I get. loading that file doesn't work. Which seems to suggest that how I'm crafting the json string written to the file with `json.dump` is problematic! But I haven't figured out /why/ ugh.

Loading…
Cancel
Save