Monday, 19 August 2013

JSON Processing Using Python

JSON Processing Using Python

I'm trying to download a json file, save the file, and iterate through the
json file in order to extract all information and save it is variables.
I'm then going to format a message in csv format to send the data to
another system. My problem is the json data. It appears to be a dictionary
within a list and I'm not sure how to process it.
Here's the json:
[ {
"ipAddress" : "",
"feedDescription" : "Botted Node Feed",
"bnFeedVersion" : "1.1.4",
"generatedTs" : "2013-08-01 12:00:10.360+0000",
"count" : 642903,
"firstDiscoveredTs" : "2013-07-21 19:07:20.627+0000",
"lastDiscoveredTs" : "2013-08-01 00:34:41.052+0000",
"threatType" : "BN",
"confidence" : 82,
"discoveryMethod" : "spamtrap",
"indicator" : true,
"supportingData" : {
"behavior" : "spamming",
"botnetName" : null,
"spamtrapData" : {
"uniqueSubjectCount" : 88
},
"p2pData" : {
"connect" : null,
"port" : null
}
}
}, {
"ipAddress" : "",
"feedDescription" : "Botted Node Feed",
"bnFeedVersion" : "1.1.4",
"generatedTs" : "2013-08-01 12:00:10.360+0000",
"count" : 28,
"firstDiscoveredTs" : "2013-07-19 03:19:08.622+0000",
"lastDiscoveredTs" : "2013-08-01 01:44:04.009+0000",
"threatType" : "BN",
"confidence" : 40,
"discoveryMethod" : "spamtrap",
"indicator" : true,
"supportingData" : {
"behavior" : "spamming",
"botnetName" : null,
"spamtrapData" : {
"uniqueSubjectCount" : 9
},
"p2pData" : {
"connect" : null,
"port" : null
}
}
}, {
"ipAddress" : "",
"feedDescription" : "Botted Node Feed",
"bnFeedVersion" : "1.1.4",
"generatedTs" : "2013-08-01 12:00:10.360+0000",
"count" : 160949,
"firstDiscoveredTs" : "2013-07-16 18:52:33.881+0000",
"lastDiscoveredTs" : "2013-08-01 03:14:59.452+0000",
"threatType" : "BN",
"confidence" : 82,
"discoveryMethod" : "spamtrap",
"indicator" : true,
"supportingData" : {
"behavior" : "spamming",
"botnetName" : null,
"spamtrapData" : {
"uniqueSubjectCount" : 3
},
"p2pData" : {
"connect" : null,
"port" : null
}
}
} ]
My code:
download = 'https:URL.BNfeed20130801.json'
request = requests.get(download, verify=False)
out = open(fileName, 'w')
for row in request:
if row.strip():
for column in row:
out.write(column)
else:
continue
out.close()
time.sleep(4)
jsonRequest = request.json()
for item in jsonRequest:
print jsonRequest[0]['ipAddress']
print jsonRequest[item]['ipAddress'] --I also tried this
When I do the above it just prints the same IP over and over again. I've
put in the print statement for testing purposes only. Once I figure out to
to access the different elements of the JSON I will store it in variables
and then use these variables accordingly. Any help is greatly appreciated.
Thanks in advance for any help. I'm using Python 2.6 on Linux.

No comments:

Post a Comment