{"id":9095,"date":"2015-12-03T12:11:38","date_gmt":"2015-12-03T10:11:38","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=9095"},"modified":"2015-11-30T18:59:12","modified_gmt":"2015-11-30T16:59:12","slug":"python-parsing-json-http-chunking-stream","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/","title":{"rendered":"Python: Parsing a JSON HTTP chunking stream"},"content":{"rendered":"<p>I\u2019ve been playing around with meetup.com\u2019s API again and this time wanted to consume the <a href=\"http:\/\/www.meetup.com\/meetup_api\/docs\/stream\/2\/rsvps\/#http\">chunked HTTP RSVP stream<\/a> and filter RSVPs for events I\u2019m interested in.<\/p>\n<p>I use Python for most of my hacking these days and if HTTP requests are required the <a href=\"http:\/\/docs.python-requests.org\/en\/latest\/\">requests library<\/a> is my first port of call.<\/p>\n<p>I started out with the following script<\/p>\n<pre class=\" brush:py\">import requests\r\nimport json\r\n\u00a0\r\ndef stream_meetup_initial():\r\n    uri = \"http:\/\/stream.meetup.com\/2\/rsvps\"\r\n    response = requests.get(uri, stream = True)\r\n    for chunk in response.iter_content(chunk_size = None):\r\n        yield chunk\r\n\u00a0\r\nfor raw_rsvp in stream_meetup_initial():\r\n    print raw_rsvp\r\n    try:\r\n        rsvp = json.loads(raw_rsvp)\r\n    except ValueError as e:\r\n        print e\r\n        continue<\/pre>\n<p>This mostly worked but I also noticed the following error from time to time:<\/p>\n<pre class=\" brush:text\">No JSON object could be decoded<\/pre>\n<p>Although less frequent, I also saw errors suggesting I was trying to parse an incomplete JSON object. I tweaked the function to keep a local buffer and only yield that if the chunk ended in a new line character:<\/p>\n<pre class=\" brush:py\">def stream_meetup_newline():\r\n    uri = \"http:\/\/stream.meetup.com\/2\/rsvps\"\r\n    response = requests.get(uri, stream = True)\r\n    buffer = \"\"\r\n    for chunk in response.iter_content(chunk_size = 1):\r\n        if chunk.endswith(\"\\n\"):\r\n            buffer += chunk\r\n            yield buffer\r\n            buffer = \"\"\r\n        else:\r\n            buffer += chunk<\/pre>\n<p>This mostly works although I\u2019m sure I\u2019ve seen some occasions where two JSON objects were being yielded and then the call to \u2018json.loads\u2019 failed. I haven\u2019t been able to reproduce that though.<\/p>\n<p>A second read through the <a href=\"http:\/\/docs.python-requests.org\/en\/latest\/user\/advanced\/#streaming-requests\">requests documentation<\/a> made me realise I hadn\u2019t read it very carefully the first time since we can make our lives much easier by using \u2018iter_lines\u2019 rather than \u2018iter_content\u2019:<\/p>\n<pre class=\" brush:py\">r = requests.get('http:\/\/stream.meetup.com\/2\/rsvps', stream=True)\r\nfor raw_rsvp in r.iter_lines():\r\n    if raw_rsvp:\r\n        rsvp = json.loads(raw_rsvp)\r\n        print rsvp<\/pre>\n<p>We can then process \u2018rsvp\u2019, filtering out the ones we\u2019re interested in.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/www.markhneedham.com\/blog\/2015\/11\/28\/python-parsing-a-json-http-chunking-stream\/\">Python: Parsing a JSON HTTP chunking stream<\/a> from our <a href=\"http:\/\/www.webcodegeeks.com\/join-us\/wcg\/\">WCG partner<\/a> Mark Needham at the <a href=\"http:\/\/www.markhneedham.com\/blog\/\">Mark Needham Blog<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve been playing around with meetup.com\u2019s API again and this time wanted to consume the chunked HTTP RSVP stream and filter RSVPs for events I\u2019m interested in. I use Python for most of my hacking these days and if HTTP requests are required the requests library is my first port of call. I started out &hellip;<\/p>\n","protected":false},"author":48,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"class_list":["post-9095","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python: Parsing a JSON HTTP chunking stream - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"I\u2019ve been playing around with meetup.com\u2019s API again and this time wanted to consume the chunked HTTP RSVP stream and filter RSVPs for events I\u2019m\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: Parsing a JSON HTTP chunking stream - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"I\u2019ve been playing around with meetup.com\u2019s API again and this time wanted to consume the chunked HTTP RSVP stream and filter RSVPs for events I\u2019m\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-12-03T10:11:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Mark Needham\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mark Needham\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/\"},\"author\":{\"name\":\"Mark Needham\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/848a54e2ee724e46069ce36c2e52e98e\"},\"headline\":\"Python: Parsing a JSON HTTP chunking stream\",\"datePublished\":\"2015-12-03T10:11:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/\"},\"wordCount\":228,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/\",\"name\":\"Python: Parsing a JSON HTTP chunking stream - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2015-12-03T10:11:38+00:00\",\"description\":\"I\u2019ve been playing around with meetup.com\u2019s API again and this time wanted to consume the chunked HTTP RSVP stream and filter RSVPs for events I\u2019m\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python: Parsing a JSON HTTP chunking stream\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/848a54e2ee724e46069ce36c2e52e98e\",\"name\":\"Mark Needham\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5489baed26ce2d932bf951ecfb47afe80bec45d3648c23521d87c83b8f1c3ea9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5489baed26ce2d932bf951ecfb47afe80bec45d3648c23521d87c83b8f1c3ea9?s=96&d=mm&r=g\",\"caption\":\"Mark Needham\"},\"sameAs\":[\"http:\/\/www.markhneedham.com\/blog\/\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/mark-needham\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: Parsing a JSON HTTP chunking stream - Web Code Geeks - 2026","description":"I\u2019ve been playing around with meetup.com\u2019s API again and this time wanted to consume the chunked HTTP RSVP stream and filter RSVPs for events I\u2019m","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/","og_locale":"en_US","og_type":"article","og_title":"Python: Parsing a JSON HTTP chunking stream - Web Code Geeks - 2026","og_description":"I\u2019ve been playing around with meetup.com\u2019s API again and this time wanted to consume the chunked HTTP RSVP stream and filter RSVPs for events I\u2019m","og_url":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2015-12-03T10:11:38+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Mark Needham","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Mark Needham","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/"},"author":{"name":"Mark Needham","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/848a54e2ee724e46069ce36c2e52e98e"},"headline":"Python: Parsing a JSON HTTP chunking stream","datePublished":"2015-12-03T10:11:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/"},"wordCount":228,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/","url":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/","name":"Python: Parsing a JSON HTTP chunking stream - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2015-12-03T10:11:38+00:00","description":"I\u2019ve been playing around with meetup.com\u2019s API again and this time wanted to consume the chunked HTTP RSVP stream and filter RSVPs for events I\u2019m","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/python\/python-parsing-json-http-chunking-stream\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python: Parsing a JSON HTTP chunking stream"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/848a54e2ee724e46069ce36c2e52e98e","name":"Mark Needham","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5489baed26ce2d932bf951ecfb47afe80bec45d3648c23521d87c83b8f1c3ea9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5489baed26ce2d932bf951ecfb47afe80bec45d3648c23521d87c83b8f1c3ea9?s=96&d=mm&r=g","caption":"Mark Needham"},"sameAs":["http:\/\/www.markhneedham.com\/blog\/"],"url":"https:\/\/www.webcodegeeks.com\/author\/mark-needham\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/9095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=9095"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/9095\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=9095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=9095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=9095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}