Error loading JSON data in python - 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.

Related

JSONDecodeError: Expecting ',' delimiter

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.

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)

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)

Unable to load Sentihood dataset Json file in Python

Sentihood Dataset is a dataset for Target Aspect-based Sentiment Analysis. Its Test and Train file are available in Json format. However, when I try loading it using the json module of python, it gives the following error-
JSONDecodeError: Expecting value: line 7 column 1 (char 6)
Is there some other way of loading Json files? I don't have much knowledge of Json and hence would appreciate any help.
Link for Sentihood dataset : https://github.com/uclmr/jack/tree/master/data/sentihood
My code is simply:
with open("sentihood-train.json", "r") as read_it:
data = json.load(read_it)
When file is reading json.loads needs file content which can be done by following way. Pass f.read() in json.loads line.
import json
with open("test.json",mode='r') as f:
d = json.loads(f.read()) # changed this line
print(d)

How do I go about parsing the contents of a large JSON file with patterns as opposed to having to manually access each entry?

I am attempting to parse this JSON file. I need to be able to access and preform a download using the contents of each hash entry. How can I do this in Python without having to manually write code to access each and every entry?
You'd use the json library to parse the data.
You must first load the data from the web and decode it to a Unicode string:
import json
from urllib.request import urlopen
response = urlopen('https://s3.amazonaws.com/Minecraft.Download/indexes/legacy.json')
# the default encoding for JSON is UTF-8, but the response can give you
# a different codec
encoding = response.info().get_content_charset('utf-8')
data = json.loads(response.read().decode(encoding))
Now you can loop through the data:
for name, info in data['objects'].items():
print('{}: {}'.format(name, info['hash']))
This produces:
lang/fr_CA.lang: 6df06576e677d952dc15da3926f4ed822d106354
sounds/random/orb.ogg: e9833a1512b57bcf88ac4fdcc8df4e5a7e9d701d
sounds/mob/villager/yes1.ogg: be73a79fb1ab966887a8b88bed9aa6b3d9173d71
sounds/mob/cat/purreow2.ogg: 08573a1f11058b09c5855122dff47ceb209f771e
sound/mob/spider/say2.ogg: 501b40b97ee55cb7a97943ee620aa05131089fc2
lang/el_GR.lang: 4330e3218548e9f3268d103e5ab71fa2b28d9b20
sound/mob/horse/soft1.ogg: a9a83e3f186b3af56a9844b8d4976c6251ba17eb
sounds/mob/bat/death.ogg: 6df3b2c2b951863fc5200edf3ff3396b105ed897
# etc.
I guess the short answer would be the json module.

Categories

Resources