Json parsing received error

Can someone help me figure out why I am receiving the error message.

Phyton code:

import json
import csv

with open("/Users/olicaluag/Desktop/capture_for_BRONEK_FILTER.txt") as file:
data = json.load(file)

fname = “oli_bronek.csv”

with open(fname, “w”) as file:
csv_file = csv.writer(file)
csv_file.writerow([“Jitter”, “Latency”, “Link”, “Packetloss”, “Timestamp”])
for result in data[“logs”]:
csv_file.writerow([item[“jitter”], item[“latency”], item[“link”], item[“packetloss”], item[“timestamp”]])

Error message received after running the code:

data = json.load(file)
File “/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/init.py”, line 293, in load
return loads(fp.read(),
File “/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/init.py”, line 346, in loads
return _default_decoder.decode(s)
File “/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py”, line 340, in decode
raise JSONDecodeError(“Extra data”, s, end)
json.decoder.JSONDecodeError: Extra data: line 5 column 30 (char 190)

json file looks like this:

{
“interface”: “port1”,
“logs”: ,
“name”: “Default_FortiGuard”
},
{
“interface”: “port1”,
“logs”: [
{
“jitter”: 0.073233,
“latency”: 15.612933,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624284672
},
{
“jitter”: 0.069133,
“latency”: 15.622801,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624284672
},
{
“jitter”: 0.059133,
“latency”: 15.622666,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624284673
},
{
“jitter”: 0.059000,
“latency”: 15.621200,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624284673
},
{
“jitter”: 0.055633,
“latency”: 15.623632,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624284674
},
{
“jitter”: 0.054100,
“latency”: 15.624532,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624284674
},
{
“jitter”: 0.165400,
“latency”: 15.660831,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624285272
}
.
.
.
.
],
“name”: “bronek”
},

I’m not an expert, but I think it is because you have multiple
objects inserted into the same JSON file. Where did you get the JSON
file from? I think it is invalid.

The docs say:

“Note Unlike pickle and marshal, JSON is not a framed protocol, so
trying to serialize multiple objects with repeated calls to dump() using
the same fp will result in an invalid JSON file.”

So I interpret that as meaning that your JSON file must contain
exactly one object, i.e. one {key:value, ...} object.

Your JSON file looks like:

{ "interface": "port1", ... },
{ "interface": "port1", ... },

so it contains at least 2 objects, not one. After reading the first
object, the load() function sees that there is still text remaining in
the file, and throws an error.

That’s my reading of what is going on. I’m not an expert, so I could be
wrong.

The problem is probably in JSON, not Python.

You can use a utility to validate the JSON. I used Free Online JSON Formatter - FreeFormatter.com. I managed to get something to work but I am sure it is not exactly what you need. The following is what I ended up with.

[{
"interface": "port1",
"logs": 0,
"name": "Default_FortiGuard"
},
{
"interface": "port1",
"logs": [
{
"jitter": 0.073233,
"latency": 15.612933,
"link": "up",
"packetloss": 0,
"timestamp": 1624284672
},
{
"jitter": 0.069133,
"latency": 15.622801,
"link": "up",
"packetloss": 0,
"timestamp": 1624284672
},
{
"jitter": 0.059133,
"latency": 15.622666,
"link": "up",
"packetloss": 0,
"timestamp": 1624284673
},
{
"jitter": 0.059000,
"latency": 15.621200,
"link": "up",
"packetloss": 0,
"timestamp": 1624284673
},
{
"jitter": 0.055633,
"latency": 15.623632,
"link": "up",
"packetloss": 0,
"timestamp": 1624284674
},
{
"jitter": 0.054100,
"latency": 15.624532,
"link": "up",
"packetloss": 0,
"timestamp": 1624284674
},
{
"jitter": 0.165400,
"latency": 15.660831,
"link": "up",
"packetloss": 0,
"timestamp": 1624285272
}
],
"name": "bronek"
}]}]

One thing that I added is an extra set of square brackets ([]) around the entire data.

i followed your link Free Online Json Formatter to fix the json file. After running the python code I received a different error which I need assistance to interpret it.

I just want also to confirm that the python code is correct and that only the json file is causing the error.

Phyton Code:

import json
import csv

with open("/Users/olicaluag/Desktop/capture_for_BRONEK_FILTER.txt") as file:
data = json.load(file)

fname = “oli2.csv”

with open(fname, “w”) as file:
csv_file = csv.writer(file)
csv_file.writerow([“Jitter”, “Latency”, “Link”, “Packetloss”, “Timestamp”])
for result in data[“logs”]:
csv_file.writerow([item[“jitter”], item[“latency”], item[“link”], item[“packetloss”], item[“timestamp”]])

Error message:
/Users/olicaluag/PycharmProjects/pythonProject/venv/bin/python /Users/olicaluag/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
File “/Users/olicaluag/PycharmProjects/pythonProject/main.py”, line 12, in
for result in data[“logs”]:
TypeError: list indices must be integers or slices, not str

Process finished with exit code 1

Json:
[
{
“interface”: “port1”,
“logs”: ,
“name”: “Default_FortiGuard”
},
{
“interface”: “port1”,
“logs”: [
{
“jitter”: 0.073233,
“latency”: 15.612933,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624284672
},
{
“jitter”: 0.069133,
“latency”: 15.622801,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624284672
},
{
“jitter”: 0.059133,
“latency”: 15.622666,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624284673
},
.
.
.
{
“jitter”: 0.1654,
“latency”: 15.660831,
“link”: “up”,
“packetloss”: 0,
“timestamp”: 1624285272
}
],
“name”: “bronek”
}
]

I created a json with the first 2 entries.

[
{
"interface": "port1",
"logs": [],
"name": "Default_FortiGuard"
},
{
"interface": "port1",
"logs": [
{
"jitter": 0.073233,
"latency": 15.612933,
"link": "up",
"packetloss": 0,
"timestamp": 1624284672
}]}
]

… and saved it as bronek.json. Read it like you did:

>>> with open("bronek.json") as json_file:
...   data = json.load(json_file)
...

Then I found out what the type of data was:

>>> type(data)
<class 'list'>

Just what the error message says.

Now what is in the items?


>>> data[0]
{'interface': 'port1', 'logs': [], 'name': 'Default_FortiGuard'}
>>> data[1]
{'interface': 'port1', 'logs': [{'jitter': 0.073233, 'latency': 15.612933, 'link': 'up', 'packetloss': 0, 'timestamp': 1624284672}]}

You have seen 2 things that in Python are basic when debugging: finding out the type of a variable, and looking at its content,. You will find these go a long way while debugging simple (and less simple) programs.

Have fun programming!