JSONDecodeError: Expecting ',' delimiter - python

I am trying to import a JSON file and convert it into a dataframe to be used for data analysis in Python. Below is my code:
import json
import pandas as pd
with open('Kickstarter_2015-06-12.json','r',encoding = "utf8") as f:
data5 = json.loads(f.read())
df_nested_list = pd.json_normalize(data5, record_path =['projects'])
However, I got this error when I run these codes:
JSONDecodeError: Expecting ',' delimiter
May I know how do I solve this problem? Any help in this area would be appreciated. Thanks!

I would suggest to use this JSON lint to make sure that your JSON is properly formatted, it seems that you're missing a comma somewhere.
It could also happen when " or ' are involved in your strings/values because these characters can mess up the parser but it's hard to tell without seeing the JSON itself. If json shows as valid in json lint and you can find the values that contain special characters please provide a small example without having to expose your entire json.

Related

CSV-File has string for every line. How can I transform it to use it for VS Code?

I am trying to import a csv-File and make a dictionary out of it.
The problem is that every line is in a string.
For example like this:
""First Name", "Last Name", "Date of Birth""
""Alex", "Turner", "08.09.1978""
""Max", "Parker", "16.02.2003""
""Mike", "Johnsen", "04.12.1999""
I tried to transform it into utf-8 because maybe thought that would be the problem but didn`t work.
import csv
csv_filename = 'Pläne.csv'
with open(csv_filename, encoding='utf-8') as f:
plaene_dict = csv.DictReader(f)
I have to delete all the double quotes at the beginning and end of every line to get a dictionary out.
Had someone this problem before? How can I transform the csv to be fine for the input.
Also have no idea to work around and to delete all the " with python before transform it into in dict. Anyone an idea?
Thanks a lot.
maxmyh
EDIT:
Here is a Link to get a sample of the file https://www.transfernow.net/dl/20221109C4Av4YIO

File not converting to JSON properly from xml

I am using Python to convert data from a xml file to json and putting it in a file. I am using xmltodict to convert to dictionary using 'parse' and then converting into json using 'dumps'. Here is the code below: -
import xmltodict
import json
with open('note.xml') as xml_file:
my_dict=xmltodict.parse(xml_file.read())
xml_file.close()
json_data=json.dumps(my_dict)
with open('note.json', 'w') as f:
json.dump(json_data, f)
Here is a sample xml file that I have used. However, in the output, I am getting something not quite json like, with the added backslash. Looks like gibberish: -
"{\"note\": {\"to\": \"Tove\", \"from\": \"Jani\", \"heading\": \"Reminder\", \"body\": \"Don't forget me this weekend!\"}}"
I am trying to understand why I am not able to get data in proper json form. Is there anything wrong with my code? Please note that I am not a very skilled programmer and have only used Python sporadically.
You need to add below code after json_data=json.dumps(my_dict) to convert string to json object
json_data = json.loads(json_data)

'End of file expected' when appending to json file

I'm relatively new to python / using JSONs and I'm having an issue.
I'm trying to add 200 of my tweets to a json file. here is my code that does this :
def process_or_store(tweet):
with open('baileighsTweets.json', 'a') as f:
f.write(json.dumps(tweet._json, indent=4))
f.close()
for tweet in tweepy.Cursor(api.user_timeline).items(200):
process_or_store(tweet)
This code runs fine, and adds my tweets to a json file, with each tweet being a json object. however, an error occurs with one of my objects in the json file :
picture with the error
same code on a different line, no error
it appears to be a very basic syntax issue but I'm confused about why it happened - my code adds to the json file, I don't do it manually, so I don't understand why I received an 'end of file expected' error, and I don't how to fix it.
Thanks in advance for your help/suggestions guys!
It would have been good if you had attached the json file along the question. I feel there might be syntax issue like missing "," or the "}" braces. You can use https://jsonlint.com/ to validate your json and try to understand where the real issue is. Hope this helps you.

How do I parse faulty json file from python using module json?

I have big size of json file to parse with python, however it's incomplete (i.e., missing parentheses in the end). The json file consist of one big json object which contains json objects inside. All json object in outer json object is complete, just finishing parenthese are missing.
for example, its structure is like this.
{bigger_json_head:value, another_key:[{small_complete_json1},{small_complete_json2}, ...,{small_complete_json_n},
So final "]}" are missing. however, each small json forms a single row so when I tried to print each line of the json file I have, I get each json object as a single string.
so I've tried to use:
with open("file.json","r",encoding="UTF-8") as f:
for line in f.readlines()
line_arr.append(line)
I expected to have a list with line of json object as its element
and then I tried below after the process:
for json_line in line_arr:
try:
json_str = json.loads(json_line)
print(json_str)
except json.decoder.JSONDecodeError:
continue
I expected from this code block, except first and last string, this code would print json string to console. However, it printed nothing and just got decode error.
Is there anyone who solved similar problem? please help. Thank you
If the faulty json file only miss the final "]}", then you can actually fix it before parse it.
Here is an example code to illustrate:
with open("file.json","r",encoding="UTF-8") as f:
faulty_json_str = f.read()
fixed_json_str = faulty_json_str + ']}'
json_obj = json.loads(fixed_json_str)

Error loading JSON data in python

I am trying to open a JSON file in python with the following code:
import json
from pprint import pprint
with open('C:\\Users\\Dave\\Desktop\\data.json') as data_file:
data = json.load(data_file)
pprint(data)
But I get the follow error.
ValueError: Expecting property name enclosed in double quotes: line 1
column 2 (char 1)
I am using the test json {'a':1}
The call to JSON is fine. You can read more about opening JSONs with python.
If you replace your JSON with one of the good JSONs from that post, you will find the it runs just fine.
All you need to do is correctly format your data.

Categories

Resources