Parsing a Python JSON -
i trying parse below json, , extract name , interval elements.
reply "[ { "interface" : [ { "name" : "ethernet39", "number" : 39, "rate" : [ { "interval" : 45, "rx-bad-vlan-rate" : 0, "rx-broadcast-packet-rate" : 0, "rx-byte-rate" : 0, "rx-drop-rate" : 0, "rx-error-rate" : 0, "rx-multicast-packet-rate" : 0, "rx-unicast-packet-rate" : 0, "timestamp" : "2015-06-18t21:59:23.703z", "tx-broadcast-packet-rate" : 0, "tx-byte-rate" : 0, "tx-drop-rate" : 0, "tx-error-rate" : 0, "tx-multicast-packet-rate" : 0, "tx-unicast-packet-rate" : 0 }, { "interval" : 45, "rx-bad-vlan-rate" : 0, "rx-broadcast-packet-rate" : 0, "rx-byte-rate" : 0, "rx-drop-rate" : 0 ...
i've tried following:
x in range(0,len(interfaces)): interface in interfaces[x]: entry = {} entry['name'] = statistic['name'] interval in statistic['rate']: entry['byte_rate'] = interval['rx-byte-rate'] entry['packet_rate'] = interval['rx-unicast-packet-rate'] entry['timestamp'] = interval['timestamp'] entry['droprate'] = interval['rx-drop-rate'] entry['errorrate'] = interval['rx-error-rate'] entries.append(entry)
however, error typeerror: string indices must integers
what doing incorrectly? in code, interfaces json acquired doing json.load()
isn't supposed interface instead of statistic ??
for x in range(0,len(interfaces)): interface in interfaces[x]: entry = {} entry['name'] = interface['name'] interval in interface['rate']: entry['byte_rate'] = interval['rx-b
Comments
Post a Comment