简体   繁体   中英

Parsing JSON with Python to get specific value

I am trying to parse JSON with Python. I am trying to get the value of "login" which is michael for "type" which is "CreateEvent".

Here's my JSON:

[
  {
    "id": "7",
    "type": "PushEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    },
    "repo": {
      "id": 2,
      "name": "myorganization/puppet",
      "url": "https://ec2"
    },
    "payload": {
      "push_id": 5,
      "size": 1,
      "distinct_size": 1,
      "ref": "refs/heads/dev",
      "head": "5584d504f971",
      "before": "e485f37ce935775846f33b",
      "commits": [
        {
          "sha": "5584cd504f971",
          "author": {
            "email": "michael.conte@gmail.ca",
            "name": "michael"
          },
          "message": "Create dev.pp",
          "distinct": true,
          "url": "https://ec2"
            }
          ]
        },
        "public": true,
    "created_at": "2018-02-20T16:15:57Z",
    "org": {
      "id": 6,
      "login": "myorganization",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    }
  },
      {
    "id": "6",
    "type": "CreateEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    },
    "repo": {
      "id": 2,
      "name": "myorganization/puppet",
      "url": "https://ec2"
    },
    "payload": {
      "ref": "dev",
      "ref_type": "branch",
      "master_branch": "master",
      "description": null,
      "pusher_type": "user"
    },
    "public": true,
    "created_at": "2018-02-20T16:15:44Z",
    "org": {
      "id": 6,
      "login": "myorganization",
      "gravatar_id": "",
      "url": "https://ec2",
  "avatar_url": "https://ec2"
    }
  },
  {
    "id": "5",
    "type": "PushEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    },
    "repo": {
      "id": 2,
      "name": "myorganization/puppet",
      "url": "https://ec2"
        },
        "payload": {
          "push_id": 3,
      "size": 1,
      "distinct_size": 1,
      "ref": "refs/heads/master",
  "head": "e485f84b875846f33b",
  "before": "f8bb87b952bfb4",
  "commits": [
    {
      "sha": "e485f37ce6f33b",
      "author": {
        "email": "michael.conte@gmail.ca",
        "name": "michael"
      },
      "message": "Create hello.pp",
      "distinct": true,
      "url": "https://ec2"
    }
  ]
    },
    "public": true,
    "created_at": "2018-02-20T15:48:42Z",
    "org": {
      "id": 6,
      "login": "myorganization",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    }
  },
  {
    "id": "4",
    "type": "CreateEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2?"
    },
    "repo": {
      "id": 2,
      "name": "myorganization/puppet",
      "url": "https://ec2"
    },
    "payload": {
      "ref": "master",
      "ref_type": "branch",
      "master_branch": "master",
      "description": null,
      "pusher_type": "user"
    },
    "public": true,
    "created_at": "2018-02-20T15:48:21Z",
    "org": {
      "id": 6,
      "login": "myorganization",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    }
  },
  {
    "id": "3",
    "type": "CreateEvent",
    "actor": {
      "id": 5,
      "login": "michael",
      "display_login": "michael",
      "gravatar_id": "",
      "url": "https://ec2",
      "avatar_url": "https://ec2"
    },
    "repo": {
      "id": 2,
  "name": "myorganization/puppet",
  "url": "https://ec2"
      },
     "payload": {
        "ref": null,
        "ref_type": "repository",
       "master_branch": "master",
        "description": null,
        "pusher_type": "user"
      },
      "public": true,
      "created_at": "2018-02-20T15:48:05Z",
      "org": {
        "id": 6,
        "login": "myorganization",
        "gravatar_id": "",
        "url": "https://ec2",
        "avatar_url": "https://ec2"
      }
        }
      ]

Here's my code:

response = requests.get(url, headers=headers, verify=False)
name = response.json()
fname = (name['type']['actor']['login'])
print(fname)

When I run the above code, I get a type error.

TypeError: list indices must be integers or slices, not str.

What am I doing wrong? I am using Python3 for my code.

Try

fname = name[0]['payload']['commits'][0]['author']['name']

The name Michael you are trying to get, is inside the dictionary named author, which is inside a single item list, which is inside the commits dictionary, which is inside the payload dictionary, which is inside a single item list.

Check out the docs for more info on collection types: http://python-textbok.readthedocs.io/en/1.0/Collections.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM