Why does this json file cause an 'expecting value' error? - python

I'm trying to load a file as JSON.
Here's the data file:
{"Title":"Assignment Checker",
"Q1_Solution":10517,
"Q2_Solution":12,
"Q3_Solution":52,
"Q4_Solution":84,
"Q5_Solution":50,
"Q6_Solution":1971,
"Q7_Solution":("Hip", "Flat", "Gambrel", "Mansard", "Shed", "Gable")}
Here's the code that fails:
f = open("checkerData.json")
checkerData = json.load(f)
f.close()
I get this error:
JSONDecodeError: Expecting value: line 8 column 16 (char 166)

Parenthesis are not valid in JSON to represent a list (array).
Fix the JSON to use [ and ] instead.
This is valid:
{
"Title": "Assignment Checker",
"Q1_Solution": 10517,
"Q2_Solution": 12,
"Q3_Solution": 52,
"Q4_Solution": 84,
"Q5_Solution": 50,
"Q6_Solution": 1971,
"Q7_Solution": [
"Hip",
"Flat",
"Gambrel",
"Mansard",
"Shed",
"Gable"
]
}

Related

Parse json file using python

I have a .json file , the first few lines are :
{
"global_id": "HICO_train2015_00000001",
"hois": [
{
"connections": [
[
0,
0
]
],
"human_bboxes": [
[
207,
32,
426,
299
]
],
"id": "153",
"invis": 0,
"object_bboxes": [
[
58,
97,
571,
404
]
]
},
I want to print out human_bboxes. id and object_bboxes.
I tried this code:
import json
# Opening JSON file
f = open('anno_list.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
s=data[0]
for i in s:
print(i[1])
# Closing file
f.close()
But, it gave me this output:
l
o
m
m
Do this:
import json
# Opening JSON file
f = open('anno_list.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
s=data[0]
# Do This:
hois_data = s["hois"][0]
print("human_bboxes",hois_data["human_bboxes"])
print("id",hois_data["id"])
print("object_bboxes",hois_data["object_bboxes"])
# Closing file
f.close()
The answer by Behdad Abdollahi Moghadam would print the answer correctly, but only for one set of bboxes and id. The below answer additionally has a for loop which parses the entire file and prints all the human and object bboxes and id into a file.
import json
# Opening JSON file
f = open('anno_list.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
f1 = open("file1.txt", "w")
for annotation in data:
f1.write("==============\n")
f1.write(annotation["global_id"])
for hois in annotation["hois"]:
f1.write("\n")
f1.write("---")
f1.write("\n")
f1.write(hois["id"])
f1.write("\n")
f1.write(str(hois["human_bboxes"]))
f1.write("\n")
f1.write(str(hois["object_bboxes"]))
f1.write("\n")
# Closing file
f.close()
f1.close()

JSON Encoding Error While Loading String from a File

After opening and before loading a json file on phython, the code end up getting a string filled with unicode blocks between every character. Its seems to be a encoding problem, any easy way to solve this problem?
import json
import io
# read file
with open('BOVA111618484700 (1).json', 'r',encoding="ASCII") as myfile:
data=myfile.read()
print(data)
# parse file
obj = json.loads(data)
print(data) shows:
[�
�{�
�"�d�a�t�a�h�o�r�a�"�:� �"�2�0�2�1�.�0�4�.�1�5� �1�1�:�0�5�:�0�0�"�,�
�"�m�i�l�i�s�e�c�o�n�d�s�"�:� �"�1�6�1�8�4�8�4�7�0�0�2�3�4�"�,�
�"�b�i�d�"�:� �"�1�1�6�.�3�2�"�,�
�"�a�s�k�"�:� �"�1�1�6�.�3�6�"�,�
�"�l�a�s�t�"�:� �"�1�1�6�.�3�2�"�,�
�"�v�o�l�u�m�e�"�:� �"�1�"�,�
�"�f�l�a�g�s�"�:� �"�2�"�
�}�,� #json string continues...
when it should show:
[
{
"datahora": "2021.04.15 11:05:00",
"miliseconds": "1618484700234",
"bid": "116.32",
"ask": "116.36",
"last": "116.32",
"volume": "1",
"flags": "2"
}, #json string continues...
After the print, the json.load function returns this error:
JSONDecodeError: Expecting value: line 1 column 2 (char 1)
Thanks #Grismar and #tevemadar the encode of the file was actually "UTF-16 LE" assigning this to the open function solve everything!
import json
import io
# read file
with open('BOVA111618484700 (1).json', 'r',encoding="UTF-16 LE") as myfile:
data=myfile.read()
print(data)
# parse file
obj = json.loads(data)

JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 6 (char 6)

getting error while executing below python code:
import json
json_str = """
{
“AircraftKey”: “AircraftKey_data”,
“Latitude”: 100,
“Longitude”: 200,
“FuelQuantityLeft”: “FuelQuantityLeft_data”,
“FuelQuantityRight”: “FuelQuantityRight_data”,
“TKSLeft”: “TKSLeft_data”,
“TKSRight”: “TKSRight_data”,
“OxygenQuantity”: “OxygenQuantity_data”,
“OilTemperature”: “OilTemperature_data”,
“Battery1Voltage”: “Battery1Voltage_data”,
“Battery2Voltage”: “Battery2Voltage_data”,
“ReceivedDate”: 20190901,
“LastUpdatedDate”: 20190901
}"""
data = json.loads(json_str)
Add this line just before json.loads:
json_str = json_str.replace('“', '"').replace('”', '"')

Python Json.loads() error ValueError: No JSON object could be decoded

I'm trying to load a JSON output file using the json.loads(). However, the script is failing with the below error. Does anyone have any idea about this?
Basically, I have a REST API GET call that output the data to a file and I would read the file in a python script and process the data independently.
I'm new to python and REST API that makes it hard to get around this. Any help is really appreciated.
#Error:
Traceback (most recent call last):
File "./HDS_Tier_Relocation_Status.py", line 40, in <module>
foo(row['storageDeviceId'], row['model'],
row['serialNumber'],row['svpIp'], row['protocol'], row['svpHost'],
row['tmServer'], ['tmPort'], row['tmAgent'], row['tmInstance'])
File "./HDS_Tier_Relocation_Status.py", line 30, in foo
pootdata = json.loads(filename) # Load JSON to a variable
File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.7/json/decoder.py", line 383, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Code:
import os
import csv
import smtplib
import re
import glob
import subprocess
import time
import json
import requests
for f in glob.glob("/home/manu/HDSRestScripts/HDSoutput*"):
os.remove(f)
# Function Definition
def foo(storageDeviceId,model,serialNumber,svpIp,protocol,svpHost,tmServer,tmPort,tmAgent,tmInstance):
filename = '/home/manu/HDSRestScripts/HDSoutput_%s_%s.json' % (storageDeviceId,svpHost)
cmd = 'curl -v -H "Accept:application/json" -H "Content-Type:application/json" -u xxx:xxx -X GET http://127.0.0.1:23450/ConfigurationManager/v1/objects/storages/%s/pools -o %s' % (storageDeviceId,filename)
os.system(cmd)
with open(filename) as json_file:
for line in json_file:
pootdata = json.loads(filename) # Load JSON to a variable
print(pooldata)
for items in pooldata['data']:
print(items['poolId'],['poolName'])
# Function call Starts
with open('/home/manu/HDSRestScripts/storageDeviceId.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
foo(row['storageDeviceId'], row['model'], row['serialNumber'],row['svpIp'], row['protocol'], row['svpHost'], row['tmServer'], ['tmPort'], row['tmAgent'], row['tmInstance'])
#Json file:
{
"data" : [ {
"poolId" : 21,
"poolStatus" : "POLN",
"usedCapacityRate" : 29,
"poolName" : "Non Perimeter",
"availableVolumeCapacity" : 665909958,
"totalPoolCapacity" : 944849304,
"numOfLdevs" : 312,
"firstLdevId" : 64770,
"warningThreshold" : 70,
"depletionThreshold" : 80,
"virtualVolumeCapacityRate" : 400,
"isMainframe" : false,
"isShrinking" : true,
"locatedVolumeCount" : 348,
"totalLocatedCapacity" : 2107885878,
"blockingMode" : "NB",
"totalReservedCapacity" : 0,
"reservedVolumeCount" : 0,
"poolActionMode" : "AUT",
"tierOperationStatus" : "MON",
"dat" : "VAL",
"poolType" : "RT",
"monitoringMode" : "CM",
"tiers" : [ {
"tierNumber" : 1,
"tierLevelRange" : "00000002",
"tierDeltaRange" : "00000005",
"tierUsedCapacity" : 56919156,
"tierTotalCapacity" : 375807600,
"tablespaceRate" : 0,
"performanceRate" : 47,
"progressOfReplacing" : 100,
"bufferRate" : 2
}, {
"tierNumber" : 2,
"tierLevelRange" : "00000000",
"tierDeltaRange" : "00000000",
"tierUsedCapacity" : 222020232,
"tierTotalCapacity" : 300147120,
"tablespaceRate" : 2,
"performanceRate" : 3,
"progressOfReplacing" : 100,
"bufferRate" : 2
}, {
"tierNumber" : 3,
"tierLevelRange" : "00000000",
"tierDeltaRange" : "00000000",
"tierUsedCapacity" : 0,
"tierTotalCapacity" : 268894584,
"tablespaceRate" : 2,
"performanceRate" : 0,
"progressOfReplacing" : 100,
"bufferRate" : 2
} ],
"duplicationNumber" : 0,
"dataReductionAccelerateCompCapacity" : 41330116310,
"dataReductionCapacity" : 0,
"dataReductionBeforeCapacity" : 0,
"dataReductionAccelerateCompRate" : 7,
"duplicationRate" : 0,
"compressionRate" : 7,
"dataReductionRate" : 0
} ]
}
Your code for reading the JSON file is incorrect. You seem to be confused about how to use json.load() and json.loads(). The former reads JSON data from a file. The latter reads it from a string. Neither of them take a filename as an argument.
Try this:
#UNTESTED
with open(filename) as json_file:
pooldata = json.load(json_file)
print(pooldata)
for items in pooldata['data']:
print(items['poolId'], items['poolName'])

Delete info from a JSON using Python

I am currently exporting a database from firebase into a JSON and want to upload this to Bigquery. However, some of the fieldnames in the database have nested information and Bigquery does not accept it this way. How can I delete 'Peripherals' from every dataset that it is present in in my JSON. It is not present in every dataset though. I provided an example of what the JSON code looks like below. Thanks for the help!
{"AppName": "DataWorks", "foundedPeripheralCount": 1, "version": "1.6.1(8056)", "deviceType": "iPhone 6", "createdAt": "2017-04-05T07:05:30.408Z", "updatedAt": "2017-04-05T07:08:49.569Z", "Peripherals": {"1CA726ED-32B1-43B4-9071-B58BBACE20A8": "Arduino"}, "connectedPeripheralCount": 1, "iOSVersion": "10.2.1"}
{"objectId": "20H5Hg2INB", "foundedPeripheralCount": 0, "DeviceVendorID": "5B7F085E-B3B6-4270-97DC-F42903CDEAC1", "version": "1.3.5(5801)", "deviceType": "iPhone 6", "createdAt": "2015-11-10T06:16:45.459Z", "updatedAt": "2015-11-10T06:16:45.459Z", "connectedPeripheralCount": 0, "iOSVersion": "9.1"}
{"AppName": "DataWorks", "foundedPeripheralCount": 2, "version": "1.6.2(8069)", "deviceType": "iPhone 6s", "createdAt": "2017-04-12T10:05:05.937Z", "updatedAt": "2017-07-06T07:33:02.006Z", "Peripherals": {"060EBAFD-3120-4AAD-8B0A-EC14A323FA25": "28902 ", "identifierInternalSensors": "Internal Sensors", "0521A273-FAA5-462E-B9EC-FBB3D60F5E99": "28895 "}, "connectedPeripheralCount": 8, "iOSVersion": "10.2.1"}
I have tried this
import json
with open('firetobq_peripheral.json') as out_file:
out = json.load(out_file)
for element in out:
del element['Peripherals']
print(out)
but I receive this error
Traceback (most recent call last):
File "editjson.py", line 3, in <module>
out = json.load(out_file)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load
**kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 369, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 629 column 1 (char 311 - 203056)
It looks like the data in 'firetobq_peripheral.json' is not valid json. If each row is on a new line you can use this code:
with open('firetobq_peripheral.json', 'r') as in_file:
dicts = []
for line in in_file.readlines() :
d = json.loads(line.strip())
if d.get('Peripherals'):
del d['Peripherals']
dicts += [d]
with open('firetobq_peripheral.json', 'w') as out_file:
out_file.write('[\n')
for i,v in enumerate(dicts):
out_file.write(json.dumps(v)+('\n' if i == len(dicts)-1 else ',\n'))
out_file.write(']')
Use this code for properly formatted json data:
with open('firetobq_peripheral.json', 'r') as in_file:
dicts = json.load(in_file)
for d in dicts:
if d.get('Peripherals'):
del d['Peripherals']
with open('firetobq_peripheral.json', 'w') as out_file:
out_file.write(json.dumps(dicts, indent=2))

Categories

Resources