我正在尝试使用json.loads()加载JSON输出文件。但是,脚本失败,出现以下错误。有没有人对此有任何想法?
基本上,我有一个REST API GET调用,它将数据输出到文件中,我会在python脚本中读取文件并独立处理数据。
我是python和REST API的新手,这使得很难解决这个问题。任何帮助都非常感谢。
#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
代码:
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
} ]
}
答案 0 :(得分:2)
您阅读JSON文件的代码不正确。您似乎对如何使用json.load()
和json.loads()
感到困惑。前者从文件中读取JSON数据。后者从字符串中读取它。他们都不以文件名作为参数。
试试这个:
#UNTESTED
with open(filename) as json_file:
pooldata = json.load(json_file)
print(pooldata)
for items in pooldata['data']:
print(items['poolId'], items['poolName'])