Not able to parse a json file, says No JSON object could be decoded
I have a json dump as
{
"alarm": [
{
"ackId": 16,
"count": 1,
"description": "<p>A SSH outage was identified on interfacen 10.21.5.39.</p> <p>A new Outage record has beenn created and service level availability calculations will ben impacted until this outage is resolved.</p>",
"firstEventTime": 1495277308427,
"id": 16,
"ifIndex": null,
"ipAddress": "10.21.5.39",
"lastEvent": {
"createTime": 1495277308437,
"description": "<p>A SSH outage was identified on interfacen 10.21.5.39.</p> <p>A new Outage record has beenn created and service level availability calculations will ben impacted until this outage is resolved.</p>",
"display": "Y",
"host": "opennms",
"id": 625,
"ifIndex": null,
"ipAddress": "10.21.5.39",
"log": "Y",
"logMessage": "SSH outage identified on interface 10.21.5.39 with reason code: Connection refused (Connection refused).",
"nodeId": 9,
"nodeLabel": "fra01-api-01",
"parameters": [
{
"name": "eventReason",
"type": "string",
"value": "Connection refused (Connection refused)"
}
],
"serviceType": {
"id": 5,
"name": "SSH"
},
"severity": "MINOR",
"source": "OpenNMS.Poller.DefaultPollContext",
"time": 1495277308427,
"uei": "uei.opennms.org/nodes/nodeLostService"
},
"lastEventTime": 1495277308427,
"logMessage": "SSH outage identified on interface 10.21.5.39 with reason code: Connection refused (Connection refused).",
"managedObjectInstance": null,
"managedObjectType": null,
"nodeId": 9,
"nodeLabel": "fra01-api-01",
"ossPrimaryKey": null,
"parameters": [
{
"name": "eventReason",
"type": "string",
"value": "Connection refused (Connection refused)"
}
],
"qosAlarmState": null,
"reductionKey": "uei.opennms.org/nodes/nodeLostService::9:10.21.5.39:SSH",
"serviceType": {
"id": 5,
"name": "SSH"
},
"severity": "MINOR",
"suppressedTime": 1495277308427,
"suppressedUntil": 1495277308427,
"type": 1,
"uei": "uei.opennms.org/nodes/nodeLostService",
"x733AlarmType": null,
"x733ProbableCause": 0
}
],
"count": 1,
"offset": null,
"totalCount": 1
}
I wrote a small code to get some details from the json
def get_nodes_opennms():
headers={'Accept': 'application/json' }
x = requests.get('http://localhost:8980/opennms/rest/alarms?comparator=ge&severity=MINOR?limit=0',headers=headers , auth=('admin', 'Op3AD'))
parsed = json.loads(x.content)
#print json.dumps(parsed, indent=4, sort_keys=True)
wriet_me_to_file = json.dumps(parsed, indent=4, sort_keys=True)
f=open('out.txt', 'w')
f.write(wriet_me_to_file)
for i in json.load(open('out.txt'))["alarm"]:
try:
print (["ipAddress"])
print (["logMessage"])
except Exception as e:
print "something is wrong"
This is throwing me error :
Traceback (most recent call last): File "python_opennms.py", line 24, in x = get_nodes_opennms() File "python_opennms.py", line 15, in get_nodes_opennms for i in json.load(open('out.txt'))["alarm"]: File "/usr/lib/python2.7/json/ init .py", line 291, in load **kw) File "/usr/lib/python2.7/json/ init .py", line 339, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
Can someone help
You are opening file that is not closed after writing. Probably system did not flush stream buffer to disk completly, so open('out.txt')
encountered file with not complete content.
Best and most secure approach would be:
with open('out.txt', 'w') as f:
f.write(wriet_me_to_file)
for i in json.load(open('out.txt'))["alarm"]:
链接地址: http://www.djcxy.com/p/48682.html
上一篇: python,写Json文件