Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 946  / 1 Year ago, fri, april 14, 2023, 7:58:00

ubuntu 19.04
python 3.7



Create a json file:



journalctl -ojson -r > a.json


Now read it in :



with open('a.json') as infile :
data = json.load(infile)

Traceback (most recent call last):
File "journalctl.json.py", line 4, in <module>
data = json.load(infile)
File "/usr/lib/python3.7/json/__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 985)


And here's line #2



{"_HOSTNAME":"vaio","_CAP_EFFECTIVE":"3fffffffff","_SELINUX_CONTEXT":"unconfined
","_COMM":"anacron","_SYSTEMD_UNIT":"anacron.service","MESSAGE":"Normal exit (0 jobs run)","_SOURCE_REALTIME_TIMESTAMP":"1570415470605362","SYSLOG_FACILITY":"9","_BOOT_ID":"06344b4eb91344bf8c1ce7c49f77ee5d","_UID":"0","PRIORITY":"5","__CURSOR":"s=2b70948bafe3422ca5fa14213e18cda1;i=3c78c;b=06344b4eb91344bf8c1ce7c49f77ee5d;m=83ae2587;t=59448d8467447;x=24f85cc398f54cab","_PID":"22196","SYSLOG_IDENTIFIER":"anacron","_SYSTEMD_INVOCATION_ID":"3cc36dc0214b4f98a311fc110648019e","__MONOTONIC_TIMESTAMP":"2209228167","_CMDLINE":"/usr/sbin/anacron -d -q -s","_SYSTEMD_CGROUP":"/system.slice/anacron.service","_MACHINE_ID":"dc7bf55ee6014b7db63fba929237b2c9","_TRANSPORT":"syslog","_GID":"0","SYSLOG_TIMESTAMP":"Oct 6 19:31:10 ","__REALTIME_TIMESTAMP":"1570415470605383","_EXE":"/usr/sbin/anacron","SYSLOG_PID":"22196","_SYSTEMD_SLICE":"system.slice"}

More From » python

 Answers
3

you received an error. json.decoder.JSONDecodeError: Extra data error. The reason is that the load method can only handle a single JSON object. And it seems that your file contains more than one JSON object. Use the following code to parse a JSON file with multiple objects



jsonData = []
with open('a.json') as f:
jsonData = [json.loads(line) for line in f]
print(jsonData)

[#4817] Saturday, April 15, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
measord

Total Points: 259
Total Questions: 131
Total Answers: 106

Location: Venezuela
Member since Sun, Oct 2, 2022
2 Years ago
;