2

What am I missing when trying to parse this JSON output with Python? The JSON looks like this:

    {
  "start": 0,
  "terms": [
    "process_name:egagent.exe"
  ],
  "highlights": [],
  "total_results": 448,
  "filtered": {},
  "facets": {},
  "results": [
    {
      "username": "SYSTEM",
      "alert_type": "test"
    },
    {
      "username": "SYSTEM2",
      "alert_type": "test"
    }
   ]
  }

The Python I'm trying to use to access this is simple. I want to grab username, but everything I try throws an error. When it doesn't throw an error, I seem to get the letter of each one. So, if I do:

apirequest = requests.get(requesturl, headers=headers, verify=False)
readable = json.loads(apirequest.content)

#print readable
for i in readable:
    print (i[0])

I get s, t, h, t, f, f, r, which are the first letters of each item. If I try i[1], I get the second letter of each item. When I try by name, say, i["start"], I get an error saying the string indices must be integers. I'm pretty confused and I am new to Python, but I haven't found anything on this yet. Please help! I just want to access the username fields, which is why I am trying to do the for loop. Thanks in advance!

4 Answers4

2

Try this:

for i in readable["results"]:
    print i["username"]
harshit-sh
  • 358
  • 1
  • 3
  • 17
2

Load your json string:

import json

s = """
{
  "start": 0,
  "terms": [
    "process_name:egagent.exe"
  ],
  "highlights": [],
  "total_results": 448,
  "filtered": {},
  "facets": {},
  "results": [
    {
      "username": "SYSTEM",
      "alert_type": "test"
    },
    {
      "username": "SYSTEM2",
      "alert_type": "test"
    }
   ]
}
"""

And print username for every result:

print [res['username'] for res in json.loads(s)['results']]

Output:

[u'SYSTEM', u'SYSTEM2']
Bahrom
  • 4,752
  • 32
  • 41
1

for i in readable will iterate i through each key in the readable dictionary. If you then print i[0], you are printing the first character of each key.

Given that you want the values associated with the "username" key in the entries of the list which is associated with the "results" key, you can get them like this:

for result in readable["results"]:
     print (result["username"])
khelwood
  • 55,782
  • 14
  • 81
  • 108
0

If readable is your JSON object (dict), you can access elements like you'd in every map, using their keys.

readable["results"][0]["username"]

Should give you "SYSTEM" string as result.

To print every username, do:

for result in readable["results"]:
    print(result["username"])

If your JSON is a str object, you have to deserialize it with json.loads(readable) first.

Jezor
  • 3,253
  • 2
  • 19
  • 43
  • Thanks! I am able to get it now, but it's limiting me to a certain number. There should be 400 or so in that output, but i'm only seeing like 10. How should I adjust the for loop to parse through each username in each result to grab each one? I've added an iterator and it's being added to on each loop, but it only grabs 10 or so. So I have for i in readable: print (readable["results"][count]["username"]) count +=1 – CluelessJSMan Jul 01 '16 at 18:10
  • You'd have to show us some more code (not the whole JSON of course, you can replace too many objects with `...`). It shouldn't matter how many `results` you've got. – Jezor Jul 01 '16 at 18:12