Why i am receiving the below error message?

Python code:

import json
import csv

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

fname = “output.csv”

with open(fname, “w”) as file:
csv_file = csv.writer(file)
csv_file.writerow([“Interface”, “Logs”, “Name”])
for item in data[“result”]:
csv_file.writerow([item[‘interface’], item[‘logs’], item[‘name’]])

error message:
Traceback (most recent call last):
File “/Users/olicaluag/Desktop/Hello World1.py”, line 5, in
data = jason.load(file)
NameError: name ‘jason’ is not defined

Python code:

import json
import csv

with open("/Users/olicaluag/Desktop/capture_for_Oliver.txt") as file:
data = jason.load(file)
[…]
error message:
Traceback (most recent call last):
File “/Users/olicaluag/Desktop/Hello World1.py”, line 5, in
data = jason.load(file)
NameError: name ‘jason’ is not defined

The traceback tells you the exact line with the problem.

It looks to me like you have misspelled “json”.

Cheers, Cameron Simpson cs@cskk.id.au

Hi Oliver,

You correctly wrote this:

import json

and then typo’ed the name:

data = jason.load(file)

Notice that the traceback tells you exactly where the error is and what
it is:

Traceback (most recent call last):
File "/Users/olicaluag/Desktop/Hello World1.py", line 5, in <module>
  data = jason.load(file)
NameError: name 'jason' is not defined

It’s not smart enough to recognise that the error is because you
misspelled “json” though. But everything else is there.

THanks, yes its a typo error, I already corrected it, however, i received new error
import json
import csv

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

fname = “output.csv”

with open(fname, “w”) as file:
csv_file = csv.writer(file)
csv_file.writerow([“jitter”, “latency”, “link”, “packetloss”, “timestamp”])
for item in data[“logs”]:
csv_file.writerow([item[‘jitter’],item[‘latency’],item[‘link’], item[‘packetloss’], item[‘timestamp’]])

output.csv was perfectly created with headers, however, its not pulling the values
Below is the error message

Traceback (most recent call last):
File “/Users/olicaluag/Desktop/Hello World1.py”, line 12, in
for item in data[“logs”]:
KeyError: ‘logs’

Thanks, I already corrected it but received a new error message. The output.csv was perfectly created plus the headers, however, there were no values per rows.

import json
import csv

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

fname = “output.csv”

with open(fname, “w”) as file:
csv_file = csv.writer(file)
csv_file.writerow([“jitter”, “latency”, “link”, “packetloss”, “timestamp”])
for item in data[“logs”]:
csv_file.writerow([item[‘jitter’],item[‘latency’],item[‘link’], item[‘packetloss’], item[‘timestamp’]])

error:Traceback (most recent call last):
File “/Users/olicaluag/Desktop/Hello World1.py”, line 12, in
for item in data[“logs”]:
KeyError: ‘logs’

THanks, yes its a typo error, I already corrected it, however, i
received new error
[…]
with open("/Users/olicaluag/Desktop/capture_for_Oliver.txt") as file:
data = json.load(file)
[…]
for item in data[“logs”]:
[…]
Traceback (most recent call last):
File “/Users/olicaluag/Desktop/Hello World1.py”, line 12, in
for item in data[“logs”]:
KeyError: ‘logs’

This tells you that there is no “logs” key in data. Print it out!

print(repr(data))

or, more readable:

from pprint import pprint
pprint(data)

Do that after you load it from the file but before you try to use it.

Cheers,
Cameron Simpson cs@cskk.id.au

Thanks Cameron,

Showing you part of the json file, which one should i consider to use for the below line command? l want to get jitter, latency, packetloss, timestamp

for item in data[“logs”]:
csv_file.writerow([item[‘jitter’],item[‘latency’],item[‘link’], item[‘packetloss’], item[‘timestamp’]])
{
“id”: 1,
“result”: [
{
“data”: [
{
“response”: {
“build”: 1828,
“http_method”: “GET”,
“name”: “sla-log”,
“path”: “virtual-wan”,
“results”: [
{
“interface”: “port1”,
“logs”: ,
“name”: “Default_DNS”
},
{
“interface”: “port1”,
“logs”: ,
“name”: “Default_Office_365”
},
{
“interface”: “port1”,
“logs”: ,
“name”: “Default_Gmail”
},
{
“interface”: “port1”,
“logs”: ,
“name”: “Default_AWS”
},
{
“interface”: “port1”,
“logs”: ,
“name”: “Default_Google Search”
},
{
“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
},

The “logs” field is buried in a tiny dict, itself in a list of similar
dicts, itself an element of an outer dict, etc etc. Supposing you had
that entire thing (from the topmost “{”) as json_data. You might print
out all the “logs” entries by iterating over the various pieces:

for result in json_data["result"]:
    for data_item in result["data"]:
        for result in data_item["response"]["results"]:
            print(result["logs"])

The for-loops are because there are lists of things inside the JSON
data.

Cheers,
Cameron Simpson cs@cskk.id.au

Many thanks Cameron, i will try this and will update you

Hi Cameron, i successfully parse the needed data to csv file, many thanks to you.

I have another json file that when i execute in my python code I am receiving error. Appreciate you could clarify to me why I am receiving the below errors:

json file:
{
“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
Python code:

import json
import csv

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

Error received:
Traceback (most recent call last):
File “/Users/olicaluag/PycharmProjects/pythonProject/bronek_json.py”, line 5, in
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)

Hi Cameron, i successfully parse the needed data to csv file, many
thanks to you.

Glad to hear it. You understand the process then?

I have another json file that when i execute in my python code I am receiving error. Appreciate you could clarify to me why I am receiving the below errors:

json file:
{
“interface”: “port1”,
“logs”: ,
“name”: “Default_FortiGuard”
},
{
[…]
json.decoder.JSONDecodeError: Extra data: line 5 column 30 (char 190)

Skipping to the error itself, this says the error is on line 5,
character 190 in the JSON file. Things looked ok to my eye so I pasted
it into a file here and brought it up in an editor. Skipping to
character 190 finds that comma in “},” above.

So, json.load expects a single JSON structure in the file (and
json.loads expects the same in whatever string you give it). The above
file has a single JSON dict up to that closing bracket on line 5.
Everything after that is extra, unexpected data, hence the exception.

Is that file complete, or cut/paste from a larger structure?

Cheers,
Cameron Simpson cs@cskk.id.au

Hi Cameron, it looks like that its just cut/paste, the second json file was just sent to me. If this is the case, what needs to do then?

Many thanks

Wel, it looked incomplete to me. The bottom looked cut off, maybe also
the top. Are you sure you have the whole thing?

if it really is a sequence of commas separated JSON dicts and complete
on that basis (i.e. all the closing brackets match up etc) you need to
part it in pieces. The JSON module itself only directly supports parsing
a single JSON object, be that a dict, a list, a string or whatever -
whatever the first object is seems in the file is what you get back. But
the load() function expects to parse the whole file, hence your error
message.

There are 2 basic approaches available to you at this point, to my mind:

The first is to cut it up yourself into distinct pieces, and parse each
piece on its own. For example, you could take each dict an put it in its
own file, then parse every file. Or you could structure the single fie
you have in some way so that you can pull out each piece on its own, and
then parse that string with the json.loads() function. For example,
there’s a format called newline delimited JSON: nothing special, it just
has each JSON object on its own on a single line of text. If you edited
your file and joined up each dict to be on one line and replaced the
comma between the dicts with a line break, you’ve have this format - one
dict per line. The you read the file by reading the file a line at a
time and using json.loads() to parse each line on its own. Think of it a
bit like CSV, but using JSON.

The next is, if the file really is a valid comma separated sequence of
JSON dicts, is just to make that a single array. So, your file looks
like:

{ ...
  ...
  ...
  },{...
  ...
  ...},{...
  ...
  ...}

Put it in brackets:

[
{ ...
  ...
  ...
  },{...
  ...
  ...},{...
  ...
  ...}
  ]

by just inserting an opening and closing square bracket at the top and
bottom. Now it is a single JSON object, a list, which contains all the
dicts. json.load() should be happy.

Cheers,
Cameron Simpson cs@cskk.id.au