-1

I'm getting an error trying to parse json file

{
0:
    {
        'data':
            [
                {
                    'state': 1,
                    'invitation_sent_at': None,
                    'group_id': None,
                    'firstname': 'goog',
                    'company': 'wbru',
                    'member_of': None,
                    'updated_at': '2017-07-21T08:07:45.375Z',
                    'last_login': None,
                    'manager_ad_id': None,
                    'distinguished_name': None,
                    'comment': '',
                    'username': 'predicted',
                    'title': '',
                    'lastname': 'hello',
                    'samaccountname': None,
                    'status': 1,
                    'locked_until': None,
                    'locale_code': None,
                    'role_id': None,
                    'userprincipalname': None,
                    'phone': '777',
                    'openid_name': 'gege',
                    'directory_id': None,
                    'id': 34486990,
                    'department': '',
                    'activated_at': None,
                    'external_id': None,
                    'created_at': '2017-07-21T08:07:45.347Z',
                    'email': 'gege@mail.ru',
                    'password_changed_at': None,
                    'invalid_login_attempts': None
                },
            ], 
        'pagination':
            {
                'before_cursor': None,
                'previous_link': None,
                'after_cursor': None,
                'next_link': None
            },
       'status':
            {
                'type': 'success',
                'message': 'Success',
                'code': 200,
                'error': False
            }
    }
}

So I need to get id. I used

dumps = json.dumps(allUsers)
print(dumps)

This will print out json file. But when I'm trying to get data or id, it shows this error

print(dumps['data'])

Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: string indices must be integers

Please, help me to get id.

DavidG
  • 24,279
  • 14
  • 89
  • 82
Ilia Levikov
  • 166
  • 1
  • 4
  • 13
  • 1
    Possible duplicate of [Parsing values from a JSON file using Python?](https://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-using-python) – arif Jul 21 '17 at 09:50
  • `dumps` is a string and `dumps['data']` makes no sense. Perhaps you want `allUsers['data']`? – Stop harming Monica Jul 21 '17 at 09:56
  • Why are you serializing your `allUsers` into a JSON string if you want to access its elements? The `json` module serves no purpose here... – zwer Jul 21 '17 at 09:58

2 Answers2

1
print(json.loads(dumps)[“data”][0]['id')

json.loads() accepts a json string. It then converts it into a python dict or a python list depending on your json. In this case it converts it into a dict.

Once you have a dict, you can access the attributes with the ["data"].

If you run into the TypeError: string indices must be integers error, that means that json.loads(dumps) is likely returning a list of dicts, and not a dict. So you may need to do the following.

print(json.loads(dumps)[0]["data"][0]["id"]

The [0] will select the first object in the list and then you can access its data attribute.

Hope that makes sense!

teewuane
  • 5,524
  • 5
  • 37
  • 43
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/16784923) – gariepy Jul 21 '17 at 21:59
  • @gariepy I'm not sure if the question changed or if I just gave a bad answer, I believe the person was originally taking a dict, dumping it to a json string, then loading that json string back into a python dict, and accessing it. That is, they already had a dict, didn't need to convert to json, then back to dict. Either way, I updated the answer, hopefully someone finds it useful ¯\_(ツ)_/¯ – teewuane Mar 02 '18 at 19:40
0

dumps is a string not a dictionnary, so you can't access 'data'

Raphaël Braud
  • 1,489
  • 10
  • 14