Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; '(of': 0.07; 'json': 0.07; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'python': 0.11; 'itself.': 0.14; '2.7.2': 0.16; 'dictionary.': 0.16; 'elements,': 0.16; 'entries.': 0.16; 'first:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:[ 60': 0.16; 'url.': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'aug': 0.22; 'load': 0.23; 'header:User-Agent:1': 0.23; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'work.': 0.31; 'getting': 0.31; 'loads': 0.31; 'file': 0.32; 'this.': 0.32; 'subject:from': 0.34; "can't": 0.35; 'skip:u 20': 0.35; 'case,': 0.35; 'date.': 0.36; 'skip:j 20': 0.36; 'url:org': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'hope': 0.61; 'first': 0.61; 'email addr:gmail.com': 0.63; 'more': 0.64; 'here': 0.66; 'header:Reply- To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'reply- to:addr:gmail.com': 0.80; 'urls,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Kev Dwyer Subject: Re: extract from json Date: Fri, 07 Mar 2014 21:05:15 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: cpc5-hari13-2-0-cust222.20-2.cable.virginm.net Mail-Copies-To: kevin.p.dwyer@gmail.com User-Agent: KNode/4.7.2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: kevin.p.dwyer@gmail.com List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394226337 news.xs4all.nl 2936 [2001:888:2000:d::a6]:47898 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68008 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