0

I have a pretty big dictionary which looks like this:

{
'startIndex': 1,
'username': 'myemail@gmail.com',
'items': [{
    'id': '67022006',
    'name': 'Adopt-a-Hydrant',
    'kind': 'analytics#accountSummary',
    'webProperties': [{
        'id': 'UA-67522226-1',
        'name': 'Adopt-a-Hydrant',
        'websiteUrl': 'https://www.udemy.com/,
        'internalWebPropertyId': '104343473',
        'profiles': [{
            'id': '108333146',
            'name': 'Adopt a Hydrant (Udemy)',
            'type': 'WEB',
            'kind': 'analytics#profileSummary'
        }, {
            'id': '132099908',
            'name': 'Unfiltered view',
            'type': 'WEB',
            'kind': 'analytics#profileSummary'
        }],
        'level': 'STANDARD',
        'kind': 'analytics#webPropertySummary'
    }]
}, {
    'id': '44222959',
    'name': 'A223n',
    'kind': 'analytics#accountSummary', 

And so on....

When I copy this dictionary on my Jupyter notebook and I run the exact same function I run on my django code it runs as expected, everything is literarily the same, in my django code I'm even printing the dictionary out then I copy it to the notebook and run it and I get what I'm expecting.

Just for more info this is the function:

google_profile = gp.google_profile # Get google_profile from DB
print(google_profile)
all_properties = []
for properties in google_profile['items']:
    all_properties.append(properties)

site_selection=[]
for single_property in all_properties:
    single_propery_name=single_property['name']
    for single_view in single_property['webProperties'][0]['profiles']:
        single_view_id = single_view['id']
        single_view_name = (single_view['name'])
        selections = single_propery_name + ' (View: '+single_view_name+' ID: '+single_view_id+')'
        site_selection.append(selections)
print (site_selection)

So my guess is that my notebook has some sort of json parser installed or something like that? Is that possible? Why in django I can't access dictionaries the same way I can on my ipython notebooks?

EDITS

More info: The error is at the line: for properties in google_profile['items']: Django debug is: TypeError at /gconnect/ string indices must be integers

Local Vars are:

all_properties =[]
current_user = 'myemail@gmail.com'
google_profile  = `the above dictionary`
Costantin
  • 2,486
  • 6
  • 31
  • 48
  • Where in the function is the error occurring? Can you show the actual error message? – Scott Hunter Mar 15 '17 at 22:23
  • Hey @ScottHunter I've added more info, thanks – Costantin Mar 15 '17 at 22:29
  • google_profile is a string, so you cannot use ['items'], this works only on dicts, defaultdicts and OrderedDicts or classes implementing __getitem__ – DevLounge Mar 15 '17 at 22:31
  • That was correct, and json solved the problem with: `import json` `s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"` `json_acceptable_string = s.replace("'", "\"")` `d = json.loads(json_acceptable_string)` `# d = {u'muffin': u'lolz', u'foo': u'kitty'}` from [Other Question](http://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary) – Costantin Mar 15 '17 at 22:36

1 Answers1

0

So just to make it clear for who finds this question:

If you save a dictionary in a database django will save it as a string, so you won't be able to access it after.

To solve this you can re-convert it to a dictionary:

The answer from this post worked perfectly for me, in other words:

import json
s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
# d = {u'muffin': u'lolz', u'foo': u'kitty'}

There are many ways to convert a string to a dictionary, this is only one. If you stumbled in this problem you can quickly check if it's a string instead of a dictionary with:

print(type(var))

In my case I had:

<class 'str'>

before converting it with the above method and then I got

<class 'dict'>

and everything worked as supposed to

Community
  • 1
  • 1
Costantin
  • 2,486
  • 6
  • 31
  • 48