-2

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
  } ]
}
Eby Jacob
  • 1,418
  • 1
  • 10
  • 28
Manu SS nair
  • 25
  • 2
  • 7
  • Are you sure there is something in the `filename`. Try printing it. I get this error, if I did not receive any data, or i receive a error. – Nikhil Wagh May 08 '18 at 22:02

1 Answers1

2

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'])
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • That worked. I realized I made a mistake there. Thanks a lot ! However, the 'pooldata' has a 'u' appearing when I try to print it. {u'data': [{u'blockingMode': u'NB', u'poolName': u'Non Perimeter', u'dataReductionRate': 0, u'duplicationNumber': 0, u'firstLdevId': 64770, u'dataReductionAccelerateCompRate': 7, u'dataReductionAccelerateCompCapacity': 41182619541, u'poolActionMode': u'AUT', u'availableVolumeCapacity': 665891394, u'isShrinking': False, u'duplicationRate': 0, u'compressionRate': 7, u'totalPoolCapacity': 944849304.... – Manu SS nair May 08 '18 at 23:06
  • Output of print(items['poolId'], items['poolName']) is (21, u'Non Perimeter') – Manu SS nair May 08 '18 at 23:09
  • Looks like the string values coming up with 'u'..is that normal ? – Manu SS nair May 08 '18 at 23:11
  • 1
    Never Mind. I figured it – Manu SS nair May 08 '18 at 23:12
  • 1
    For others who come across these comments, the `u` prefix to a string [means it's a unicode string](https://stackoverflow.com/a/2464968/584676) in Python. – codewario May 18 '22 at 17:58