Python Forum

Full Version: iterate json and count
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I've a problem with a json to print all my results.. Seems a stupid problem but..

            r = requests.post(
                request_url,
                headers=headers,
                verify=True,
                auth=(api_login, api_password)
            ).json()
In fact what I want to do is to iterate this json, grep "__name" value and count how many this value is present.

If I do:

#            print (*r.items(), sep='\n')
I can see all results.

But I don't succeed to do what I want:

If I do:
            for key,value in r.items():
                print (value[0]["attrs"]["__name"])
I can see the first element but never all elements. (one line per line)

My json is like this:
{
  "results": [
    { 
      "attrs": {
        "__name": "name1",
        "active": true
    },
    { 
      "attrs": {
        "__name": "name2",
        "active": true
    },
....
Thanks for help
Alex
for item in r['results']:
    print(item['attrs']['__name'])
I feel stupid...
Thanks, it works!