Note that there are some explanatory texts on larger screens.

plurals
  1. POIterating and printing JSON objects in Python
    primarykey
    data
    text
    <p>I have a Google Spreadsheet that I'd like to get in Python, then process it through JSON. It's halfway working, and after looking through Stackoverflow for several hours, I think it's time to ask a question. </p> <p>For example, the format of the JSON file looks like this (from <a href="https://developers.google.com/gdata/docs/json" rel="nofollow">https://developers.google.com/gdata/docs/json</a>).</p> <pre><code>{ "version": "1.0", "encoding": "UTF-8", "feed": { "xmlns": "http://www.w3.org/2005/Atom", "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/", "xmlns$gd": "http://schemas.google.com/g/2005", "xmlns$gCal": "http://schemas.google.com/gCal/2005", "id": {"$t": "..."}, "updated": {"$t": "2006-11-12T21:25:30.000Z"}, "title": { "type": "text", "$t": "Google Developer Events" }, "subtitle": { "type": "text", "$t": "The calendar contains information about upcoming developer conferences at which Google will be speaking, along with other developer-related events." }, "link": [{ "rel": "...", "type": "application/atom+xml", "href": "..." },{ "rel": "self", "type": "application/atom+xml", "href": "..." }], "author": [{ "name": {"$t": "Google Developer Calendar"}, "email": {"$t": "developer-calendar@google.com"} }], "generator":{ "version": "1.0", "uri": "http://www.google.com/calendar", "$t": "Google Calendar" }, "openSearch$startIndex": {"$t": "1"}, "openSearch$itemsPerPage": {"$t": "25"}, "gCal$timezone": {"value": "America/Los_Angeles"}, "entry": [{ "id": {"$t": "..."}, "published": {"$t": "2006-11-12T21:25:30.000Z"}, "updated": {"$t": "2006-11-12T21:25:30.000Z"}, "category": [{ "scheme": "...", "term": "..." }], "title":{ "type": "text", "$t": "WebmasterWorld PubCon 2006: Google Developer Tools in General" }, "content": { "type": "text", "$t": "Google is sponsoring at &lt;a href=\"http://www.pubcon.com/\"&gt;WebmasterWorld PubCon 2006&lt;/a&gt;. \n Come and visit us at the booth or join us for an evening demo reception where we will be talking \"5 ways to enhance your website with Google Code\". \n After all, \n it is Vegas, baby! See you soon." }, "link": [{ "rel": "alternate", "type": "text/html", "href": "...", "title": "alternate" },{ "rel": "self", "type": "application/atom+xml", "href": "..." }], "author": [{ "name": {"$t": "Google Developer Calendar"}, "email": {"$t": "developer-calendar@google.com"} }], "gd$transparency": {"value": "http://schemas.google.com/g/2005#event.opaque"}, "gd$eventStatus": {"value": "http://schemas.google.com/g/2005#event.confirmed"}, "gd$comments": {"gd$feedLink": {"href": "..."}}, "gCal$sendEventNotifications": {"value": "true"}, "gd$when": [{ "startTime": "2006-11-15", "endTime": "2006-11-17", "gd$reminder": [{"minutes": "10"}] }], "gd$where": [{"valueString": "3150 Paradise Road,Las Vegas,NV 89109"}]}, }] } } </code></pre> <p>My Python code is as follows:</p> <pre><code>import requests, json r = requests.get('link-to-google-spreadsheet-json') j = r.json() </code></pre> <p>Testing the top-level hierarchy of the JSON file outputs as follows:</p> <pre><code>&gt;&gt;&gt; print j["version"] 1.0 </code></pre> <p>But iterating over the objects, like so: </p> <pre><code>for feed in j["feed"]: for entry in feed["entry"]: for title in entry["title"]: print title["$t"] print print </code></pre> <p>Gives me the following error: </p> <pre><code>Traceback (most recent call last): File "&lt;console&gt;", line 2, in &lt;module&gt; TypeError: string indices must be integers. </code></pre> <p>It sounds like it wants to print a single letter from the string, provided I give the index number. So how do I parse the JSON so that it correctly outputs what I want (e.g. feed -> entry -> title -> $t)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload