extract from json

T

teddybubu

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?
 
K

Kev Dwyer

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.
.... json_data = json.load(f)
....{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
 
T

teddybubu

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 numberat
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.
... json_data = json.load(f){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',
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
 
T

thrinaxodon666

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.
 
C

Chris Angelico

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
 
M

Mark Lawrence

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.
 
C

Chris Angelico

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top