Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68006 > unrolled thread

extract from json

Started byteddybubu@gmail.com
First post2014-03-07 12:27 -0800
Last post2014-03-09 00:19 +1100
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  extract from json teddybubu@gmail.com - 2014-03-07 12:27 -0800
    Re: extract from json Kev Dwyer <kevin.p.dwyer@gmail.com> - 2014-03-07 21:05 +0000
      Re: extract from json teddybubu@gmail.com - 2014-03-07 19:02 -0800
    Re: extract from json thrinaxodon666@gmail.com - 2014-03-07 19:21 -0800
      Re: extract from json Chris Angelico <rosuav@gmail.com> - 2014-03-08 14:49 +1100
      spam (wasRe: extract from json) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-08 13:13 +0000
      Re: spam (wasRe: extract from json) Chris Angelico <rosuav@gmail.com> - 2014-03-09 00:19 +1100

#68006 — extract from json

Fromteddybubu@gmail.com
Date2014-03-07 12:27 -0800
Subjectextract from json
Message-ID<d72d9e78-ee11-4833-b3b2-e90ab52a6d4c@googlegroups.com>
I can't find any example on how to do this.
I have a json file like so:
{"bostock":[{"url":"http://bl.ocks.org/mbostock/9360565","title":"titleplaceholder","date":"dateplaceholder"},
{"url":"http://bl.ocks.org/mbostock/9265674","title":"titleplaceholder","date":"dateplaceholder"},
{"url":"http://bl.ocks.org/mbostock/9265467","title":"titleplaceholder","date":"dateplaceholder"},
{"url":"http://bl.ocks.org/mbostock/9234731","title":"titleplaceholder","date":"dateplaceholder"},
{"url":"http://bl.ocks.org/mbostock/9232962","title":"titleplaceholder","date":"dateplaceholder"},

this goes on for more than 700 entries. only thing unique is the number at the end of the url. I am going to load the url in python, get the date and title and write it in the json itself. 
Right now I am stuck on just reading the url in the json. Here is my code:

import json

with open("bostock.json") as json_file:
    json_data = json.load(json_file)
    print(json_data)

I have tried json_data[0], json_data.url and a few others I forget right now and it does not seem to work.  

I have already figured out how to get the title and date.
First things first: How can i just get the url for each line of the above json file? 

[toc] | [next] | [standalone]


#68008

FromKev Dwyer <kevin.p.dwyer@gmail.com>
Date2014-03-07 21:05 +0000
Message-ID<mailman.7908.1394226337.18130.python-list@python.org>
In reply to#68006
teddybubu@gmail.com wrote:

> I can't find any example on how to do this.
> I have a json file like so:
> {"bostock":
[{"url":"http://bl.ocks.org/mbostock/9360565","title":"titleplaceholder","date":"dateplaceholder"},
> 
{"url":"http://bl.ocks.org/mbostock/9265674","title":"titleplaceholder","date":"dateplaceholder"},
> 
{"url":"http://bl.ocks.org/mbostock/9265467","title":"titleplaceholder","date":"dateplaceholder"},
> 
{"url":"http://bl.ocks.org/mbostock/9234731","title":"titleplaceholder","date":"dateplaceholder"},
> 
{"url":"http://bl.ocks.org/mbostock/9232962","title":"titleplaceholder","date":"dateplaceholder"},
> 
> this goes on for more than 700 entries. only thing unique is the number at
> the end of the url. I am going to load the url in python, get the date and
> title and write it in the json itself. Right now I am stuck on just
> reading the url in the json. Here is my code:
> 
> import json
> 
> with open("bostock.json") as json_file:
>     json_data = json.load(json_file)
>     print(json_data)
> 
> I have tried json_data[0], json_data.url and a few others I forget right
> now and it does not seem to work.
> 
> I have already figured out how to get the title and date.
> First things first: How can i just get the url for each line of the above
> json file?


Hello 

Try:

Python 2.7.2 (default, Aug 19 2011, 20:41:43) [GCC] on linux2                                                           
Type "help", "copyright", "credits" or "license" for more information.                                                  

>>> import  json
>>> with open('/tmp/bostock.json') as f:
...     json_data = json.load(f)
... 
>>> json_data
{u'bostock': [{u'url': u'http://bl.ocks.org/mbostock/9360565', u'date': 
u'dateplaceholder', u'title': u'titleplaceholder'}, {u'url': 
u'http://bl.ocks.org/mbostock/9265674', u'date': u'dateplaceholder', 
u'title': u'titleplaceholder'}, {u'url': 
u'http://bl.ocks.org/mbostock/9265467', u'date': u'dateplaceholder', 
u'title': u'titleplaceholder'}, {u'url': 
u'http://bl.ocks.org/mbostock/9234731', u'date': u'dateplaceholder', 
u'title': u'titleplaceholder'}, {u'url': 
u'http://bl.ocks.org/mbostock/9232962', u'date': u'dateplaceholder', 
u'title': u'titleplaceholder'}]}

>>> urls = [x['url'] for x in json_data['bostock']]
>>> urls
[u'http://bl.ocks.org/mbostock/9360565', 
u'http://bl.ocks.org/mbostock/9265674', 
u'http://bl.ocks.org/mbostock/9265467', 
u'http://bl.ocks.org/mbostock/9234731', 
u'http://bl.ocks.org/mbostock/9232962']

Python loads the json in the file into a dictionary.  In this case, the 
dictionary has a single key, 'bostock', and the value in the dictionary for 
that key is a list (of dictionaries).  

To get the urls, you need to get the list 

json_data['bostock']

 and then iterate over it's elements, getting the value for the key url for 
each one.  
This is what the list comprehension 

[x['url'] for x in json_data['bostock']]

does.

I hope that helps,

Kev

[toc] | [prev] | [next] | [standalone]


#68018

Fromteddybubu@gmail.com
Date2014-03-07 19:02 -0800
Message-ID<0d5a61a6-f765-4865-bc2f-cc34291dffd3@googlegroups.com>
In reply to#68008
On Friday, March 7, 2014 3:05:15 PM UTC-6, Kev Dwyer wrote:
>  wrote:
> > I can't find any example on how to do this.
> 
> > I have a json file like so:
> 
> > {"bostock":[{"url":"http://bl.ocks.org/mbostock/9360565","title":"titleplaceholder","date":"dateplaceholder"},{"url":"http://bl.ocks.org/mbostock/9265674","title":"titleplaceholder","date":"dateplaceholder"},{"url":"http://bl.ocks.org/mbostock/9265467","title":"titleplaceholder","date":"dateplaceholder"},{"url":"http://bl.ocks.org/mbostock/9234731","title":"titleplaceholder","date":"dateplaceholder"},{"url":"http://bl.ocks.org/mbostock/9232962","title":"titleplaceholder","date":"dateplaceholder"},
> > this goes on for more than 700 entries. only thing unique is the number at
> 
> > the end of the url. I am going to load the url in python, get the date and
> 
> > title and write it in the json itself. Right now I am stuck on just
> 
> > reading the url in the json. Here is my code:
> > import json
> > with open("bostock.json") as json_file:
> 
> >     json_data = json.load(json_file)
> 
> >     print(json_data)
> > I have tried json_data[0], json_data.url and a few others I forget right
> 
> > now and it does not seem to work.
> > I have already figured out how to get the title and date.
> 
> > First things first: How can i just get the url for each line of the above
> 
> > json file?
> Hello 
> Try:
> 
> Python 2.7.2 (default, Aug 19 2011, 20:41:43) [GCC] on linux2                                                           
> 
> Type "help", "copyright", "credits" or "license" for more information.                                                  

> >>> import  json
> 
> >>> with open('/tmp/bostock.json') as f:
> 
> ...     json_data = json.load(f)
> >>> json_data
> {u'bostock': [{u'url': u'http://bl.ocks.org/mbostock/9360565', u'date': 
> u'dateplaceholder', u'title': u'titleplaceholder'}, {u'url': 
> u'http://bl.ocks.org/mbostock/9265674', u'date': u'dateplaceholder', 
> u'title': u'titleplaceholder'}, {u'url': 
> u'http://bl.ocks.org/mbostock/9265467', u'date': u'dateplaceholder', 
> 
> u'title': u'titleplaceholder'}, {u'url': 
> 
> u'http://bl.ocks.org/mbostock/9234731', u'date': u'dateplaceholder', 
> u'title': u'titleplaceholder'}, {u'url': 
> u'http://bl.ocks.org/mbostock/9232962', u'date': u'dateplaceholder', 
> u'title': u'titleplaceholder'}]} 
> >>> urls = [x['url'] for x in json_data['bostock']]
> 
> >>> urls
> 
> [u'http://bl.ocks.org/mbostock/9360565', 
> 
> u'http://bl.ocks.org/mbostock/9265674', 
> 
> u'http://bl.ocks.org/mbostock/9265467', 
> 
> u'http://bl.ocks.org/mbostock/9234731', 

> u'http://bl.ocks.org/mbostock/9232962']
 
> Python loads the json in the file into a dictionary.  In this case, the 
> dictionary has a single key, 'bostock', and the value in the dictionary for 
 
> that key is a list (of dictionaries).  

> To get the urls, you need to get the list 
 
> json_data['bostock']
>  and then iterate over it's elements, getting the value for the key url for 
 > each one.  
> This is what the list comprehension 
> [x['url'] for x in json_data['bostock']]
> does.
> I hope that helps, 
> Kev

Kev your the man. Thanks

[toc] | [prev] | [next] | [standalone]


#68019

Fromthrinaxodon666@gmail.com
Date2014-03-07 19:21 -0800
Message-ID<c80f6c49-ac85-4ba4-8ca9-b305a1f4f84e@googlegroups.com>
In reply to#68006
On Friday, March 7, 2014 3:27:27 PM UTC-5, tedd...@gmail.com wrote:
> I can't find any example on how to do this.
> 
> I have a json file like so:
> 
> {"bostock":[{"url":"http://bl.ocks.org/mbostock/9360565","title":"titleplaceholder","date":"dateplaceholder"},
> 
> {"url":"http://bl.ocks.org/mbostock/9265674","title":"titleplaceholder","date":"dateplaceholder"},
> 
> {"url":"http://bl.ocks.org/mbostock/9265467","title":"titleplaceholder","date":"dateplaceholder"},
> 
> {"url":"http://bl.ocks.org/mbostock/9234731","title":"titleplaceholder","date":"dateplaceholder"},
> 
> {"url":"http://bl.ocks.org/mbostock/9232962","title":"titleplaceholder","date":"dateplaceholder"},
> 
> 
> 
> this goes on for more than 700 entries. only thing unique is the number at the end of the url. I am going to load the url in python, get the date and title and write it in the json itself. 
> 
> Right now I am stuck on just reading the url in the json. Here is my code:
> 
> 
> 
> import json
> 
> 
> 
> with open("bostock.json") as json_file:
> 
>     json_data = json.load(json_file)
> 
>     print(json_data)
> 
> 
> 
> I have tried json_data[0], json_data.url and a few others I forget right now and it does not seem to work.  
> 
> 
> 
> I have already figured out how to get the title and date.
> 
> First things first: How can i just get the url for each line of the above json file?

I think it's better if you f*ck off.

[toc] | [prev] | [next] | [standalone]


#68022

FromChris Angelico <rosuav@gmail.com>
Date2014-03-08 14:49 +1100
Message-ID<mailman.7919.1394250583.18130.python-list@python.org>
In reply to#68019
On Sat, Mar 8, 2014 at 2:21 PM,  <thrinaxodon666@gmail.com> wrote:
> I think it's better if you (CENSORED) off.

Teddybubu, please understand that the above comment is from a spammer
and does not reflect the prevailing attitude of this list. I don't
like to make content-free posts like this, but as you already have the
answer you need, there's not a lot for me to add :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#68032 — spam (wasRe: extract from json)

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-03-08 13:13 +0000
Subjectspam (wasRe: extract from json)
Message-ID<mailman.7926.1394284433.18130.python-list@python.org>
In reply to#68019
On 08/03/2014 03:49, Chris Angelico wrote:
> On Sat, Mar 8, 2014 at 2:21 PM,  <thrinaxodon666@gmail.com> wrote:
>> I think it's better if you (CENSORED) off.
>
> Teddybubu, please understand that the above comment is from a spammer
> and does not reflect the prevailing attitude of this list. I don't
> like to make content-free posts like this, but as you already have the
> answer you need, there's not a lot for me to add :)
>
> ChrisA
>

This particular PITA of a spammer is one of the very few that I see on 
Thunderbird via gmane.  I believe that Terry Reedy amongst others does a 
good job of keeping us relatively spam free.  Thanks all.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#68033 — Re: spam (wasRe: extract from json)

FromChris Angelico <rosuav@gmail.com>
Date2014-03-09 00:19 +1100
SubjectRe: spam (wasRe: extract from json)
Message-ID<mailman.7927.1394284752.18130.python-list@python.org>
In reply to#68019
On Sun, Mar 9, 2014 at 12:13 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 08/03/2014 03:49, Chris Angelico wrote:
>>
>> On Sat, Mar 8, 2014 at 2:21 PM,  <thrinaxodon666@gmail.com> wrote:
>>>
>>> I think it's better if you (CENSORED) off.
>>
>>
>> Teddybubu, please understand that the above comment is from a spammer
>> and does not reflect the prevailing attitude of this list. I don't
>> like to make content-free posts like this, but as you already have the
>> answer you need, there's not a lot for me to add :)
>>
>> ChrisA
>>
>
> This particular PITA of a spammer is one of the very few that I see on
> Thunderbird via gmane.  I believe that Terry Reedy amongst others does a
> good job of keeping us relatively spam free.  Thanks all.

Normally I ignore him (and yes, I see those posts too, in Gmail).
Stand-alone posts aren't an issue. I just didn't want a new poster to
see that reply go through unchallenged.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web