Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterate json and count
#1
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
Reply
#2
for item in r['results']:
    print(item['attrs']['__name'])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I feel stupid...
Thanks, it works!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,321 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 51,481 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85
  iterate and print json datas enigma619 1 1,921 Apr-01-2020, 11:48 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020