JSON reading help (i think I am nearly there)

This is the place for queries that don't fit in any of the other categories.

JSON reading help (i think I am nearly there)

Postby AEA » Thu May 23, 2013 1:51 am

REMOVED CONTENT
Last edited by AEA on Fri May 24, 2013 2:09 pm, edited 1 time in total.
AEA
 
Posts: 32
Joined: Thu Apr 18, 2013 11:37 am

Re: JSON reading help (i think I am nearly there)

Postby snippsat » Thu May 23, 2013 7:17 pm

You have to look at what data "text1" give you back.
After json.loads() you get a Python dictionary/list structure back if it works.
Then is normal use of dictionary/list to get value you want.
Example:
Code: Select all
>>> json_data = {'rows': [{'hash': u'4f18bad', 'id': '9999999'}]}
>>> json_data['rows'][0]['id']
'9999999'
>>>
User avatar
snippsat
 
Posts: 168
Joined: Thu Feb 21, 2013 12:04 am

Re: JSON reading help (i think I am nearly there)

Postby AEA » Fri May 24, 2013 12:39 am

Hi thanks for the reply, this does certainly help with my understanding however is still cant quite get the data I require from the Json.

Here is the code I get as a result of
Code: Select all
print text1
without searching for something specific.

Code: Select all
{u'rows': [{u'cell': {u'username': u'<a onclick=\'newPopup(this.href,"big"); return false\' href=\'/control/profile/view/1048\' title=\'23/05/13 WIND FIRE 2/1\'><img src=\'/assets/uploads/profile/image7.jpg\'  style=\'width:22px; height:22px;\' />&nbsp;&nbsp;Breenie</a>', u'roi': u'6%', u'form': u'<span style="color:red">L</span>-<span style="color:red">L</span>-<span style="color:green">20/1</span>-<span style="color:red">L</span>-<span style="color:green">2/1</span>', u'rank': 1, u'num_selections': u'5', u'total_pl': u'19.00', u'num_win_selections': u'2'}, u'id': u'1048'}], u'total': 327, u'page': u'1'}


I know
Code: Select all
print text1 ['rows'][0]['id']
returns 1048 which i believes references the second '1048' in the data. How would I return for instance the roi of 6% or the num_selections of 5? Looking specifically at the roi ;
Code: Select all
['rows'][0]['roi']
doesnt work. So I tried to chenge to ['rows'][0]['roi'], ['cell'][0]['roi'], ['username'][0]['roi'] but to no avail.

I wonder if you could point me in the right direction for accessing data other than the id.

Many thanks

AEA
AEA
 
Posts: 32
Joined: Thu Apr 18, 2013 11:37 am

Re: JSON reading help (i think I am nearly there)

Postby metulburr » Fri May 24, 2013 12:54 am

doesnt work. So I tried to chenge to ['rows'][0]['roi'], ['cell'][0]['roi'], ['username'][0]['roi'] but to no avail.

Code: Select all
dicter = {u'rows': [{u'cell': {u'username': u'<a onclick=\'newPopup(this.href,"big"); return false\' href=\'/control/profile/view/1048\' title=\'23/05/13 WIND FIRE 2/1\'><img src=\'/assets/uploads/profile/image7.jpg\'  style=\'width:22px; height:22px;\' />&nbsp;&nbsp;Breenie</a>', u'roi': u'6%', u'form': u'<span style="color:red">L</span>-<span style="color:red">L</span>-<span style="color:green">20/1</span>-<span style="color:red">L</span>-<span style="color:green">2/1</span>', u'rank': 1, u'num_selections': u'5', u'total_pl': u'19.00', u'num_win_selections': u'2'}, u'id': u'1048'}], u'total': 327, u'page': u'1'}

print(dicter['rows'][0]['cell']['roi'])
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1415
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: JSON reading help (i think I am nearly there)

Postby AEA » Fri May 24, 2013 1:13 am

Thanks Metalburr!

So I definitely think i can get the data I require now from this Json, however could you explain to me why for roi it needs to be in the 4th set of brackets compared to for id where it needs to be in the 3rd set of brackets? I can only assume it has something to do with the curly brackets? As an example for my understanding can you tell me what would be required to return 'total' as in '327'

i tried
Code: Select all
print(text1['rows'][0]['cell']['id']['total'])
but this doesnt work, which suggests to me i havn't quite grasped the concept.

many thanks

AEA
AEA
 
Posts: 32
Joined: Thu Apr 18, 2013 11:37 am

Re: JSON reading help (i think I am nearly there)

Postby AEA » Fri May 24, 2013 1:28 am

I also notice there is no way to endorse people on this forum as a thankyou, so if either Metalburr or snippsat would like a website like, a follower on twitter or a linkedin endorsement then let me know. Thanks again guys
AEA
 
Posts: 32
Joined: Thu Apr 18, 2013 11:37 am

Re: JSON reading help (i think I am nearly there)

Postby snippsat » Fri May 24, 2013 1:47 am

I can only assume it has something to do with the curly brackets?

You have to read and understand how a dictionary work.
Code: Select all
>>> my_dict = {'key': 'value', 'id': 99999}
>>> my_dict['key']
'value'
>>> my_dict['id']
99999

can you tell me what would be required to return 'total' as in '327'

Value 327 is in outer dictionary.
So it`s as simple as json_data['total'] to get '327'.
Code: Select all
>>> json_data
{u'page': u'1',
 u'rows': [{u'cell': {u'form': u'<span style="color:red">L</span>-<span style="color:red">L</span>-<span style="color:green">20/1</span>-<span style="color:red">L</span>-<span style="color:green">2/1</span>',
                      u'num_selections': u'5',
                      u'num_win_selections': u'2',
                      u'rank': 1,
                      u'roi': u'6%',
                      u'total_pl': u'19.00',
                      u'username': u'<a onclick=\'newPopup(this.href,"big"); return false\' href=\'/control/profile/view/1048\' title=\'23/05/13 WIND FIRE 2/1\'><img src=\'/assets/uploads/profile/image7.jpg\'  style=\'width:22px; height:22px;\' />&nbsp;&nbsp;Breenie</a>'},
            u'id': u'1048'}],
 u'total': 327}

Code: Select all
>>> json_data['total']
327

Keys in json_data.
Code: Select all
>>> json_data.keys()
[u'total', u'rows', u'page']
>>> json_data.keys()[0]
u'total'
User avatar
snippsat
 
Posts: 168
Joined: Thu Feb 21, 2013 12:04 am

Re: JSON reading help (i think I am nearly there)

Postby AEA » Fri May 24, 2013 9:48 am

Perfect thanks very much :) I also notice there is no way to endorse people on this forum as a thankyou, so if either Metalburr or snippsat would like a website like, a follower on twitter or a linkedin endorsement then let me know. Thanks again guys ?????
AEA
 
Posts: 32
Joined: Thu Apr 18, 2013 11:37 am

Re: JSON reading help (i think I am nearly there)

Postby hrs » Fri May 24, 2013 2:20 pm

How about endorsing python-forum.org by not deleting your question after it has been answered?
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 3 guests

cron