{"id":1297,"date":"2012-10-06T11:56:28","date_gmt":"2012-10-06T11:56:28","guid":{"rendered":"https:\/\/www.pythonforbeginners.com\/?p=1297"},"modified":"2020-08-27T09:21:22","modified_gmt":"2020-08-27T14:21:22","slug":"parse-json-objects-in-python","status":"publish","type":"post","link":"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python","title":{"rendered":"Parse JSON objects in Python"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>In this post we will explain how you can parse JSON objects in Python.<\/p>\n<p>Knowing how to parse JSON objects is useful when you want to access an API<br \/>\nfrom various web services that gives the response in JSON.<\/p>\n<h2>Getting Started<\/h2>\n<p>First thing you have to do, is to find an URL to call the API.<\/p>\n<p>In my example, I will use the Twitter API.<\/p>\n<p>Start with importing the modules that we need for the program.<\/p>\n<pre><code class=\"\u201clanguage-python\u201d\">import json\nimport urllib2\n<\/code><\/pre>\n<p>Open the URL and the screen name.<\/p>\n<pre><code class=\"\u201clanguage-python\u201d\">url = \"http:\/\/api.twitter.com\/1\/statuses\/user_timeline.json?screen_name=wordpress\"\n<\/code><\/pre>\n<p>Print out the result<\/p>\n<pre><code class=\"\u201clanguage-python\u201d\">print data\n<\/code><\/pre>\n<h2>Using the Twitter API to parse data<\/h2>\n<p>This is a very simple program, just to give you an idea of how it works.<\/p>\n<pre><code class=\"\u201clanguage-python\u201d\">#Importing modules\nimport json\nimport urllib2\n\n# Open the URL and the screen name\nurl = \"http:\/\/api.twitter.com\/1\/statuses\/user_timeline.json?screen_name=wordpress\"\n\n# This takes a python object and dumps it to a string which is a JSON representation of that object\ndata = json.load(urllib2.urlopen(url))\n\n#print the result\nprint data\n<\/code><\/pre>\n<p>If you are interested to see another example of how to use JSON in Python, please<br \/>\nhave a look at the <a href=\"https:\/\/www.pythonforbeginners.com\/code-snippets-source-code\/imdb-crawler\">&#8220;IMDB Crawler&#8221; script<\/a>.<\/p>\n<p>To use the Twitter API, see the official documentation on Twitter.<br \/>\n<a title=\"twitter-docs\" href=\"https:\/\/dev.twitter.com\/docs\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/dev.twitter.com\/docs<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview In this post we will explain how you can parse JSON objects in Python. Knowing how to parse JSON objects is useful when you want to access an API from various web services that gives the response in JSON. Getting Started First thing you have to do, is to find an URL to call [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9,61,94],"tags":[],"class_list":{"0":"post-1297","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-code-snippets-source-code","7":"category-json","8":"category-python-on-the-web","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Parse JSON objects in Python - PythonForBeginners.com<\/title>\n<meta name=\"description\" content=\"Parse JSON objects in Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\" \/>\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.pythonforbeginners.com\/json\/parse-json-objects-in-python\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Parse JSON objects in Python - PythonForBeginners.com\" \/>\n<meta property=\"og:description\" content=\"Parse JSON objects in Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python\" \/>\n<meta property=\"og:site_name\" content=\"PythonForBeginners.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/pythonbeginners\" \/>\n<meta property=\"article:published_time\" content=\"2012-10-06T11:56:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-27T14:21:22+00:00\" \/>\n<meta name=\"author\" content=\"PFB Staff Writer\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonbeginners\" \/>\n<meta name=\"twitter:site\" content=\"@pythonbeginners\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PFB Staff Writer\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/json\\\/parse-json-objects-in-python#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/json\\\/parse-json-objects-in-python\"},\"author\":{\"name\":\"PFB Staff Writer\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/person\\\/1f49841b9ba76108d92e7ef1ca5ee648\"},\"headline\":\"Parse JSON objects in Python\",\"datePublished\":\"2012-10-06T11:56:28+00:00\",\"dateModified\":\"2020-08-27T14:21:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/json\\\/parse-json-objects-in-python\"},\"wordCount\":156,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\"},\"articleSection\":[\"Code Snippets\",\"Json\",\"Python On The Web\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonforbeginners.com\\\/json\\\/parse-json-objects-in-python#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/json\\\/parse-json-objects-in-python\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/json\\\/parse-json-objects-in-python\",\"name\":\"Parse JSON objects in Python - PythonForBeginners.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#website\"},\"datePublished\":\"2012-10-06T11:56:28+00:00\",\"dateModified\":\"2020-08-27T14:21:22+00:00\",\"description\":\"Parse JSON objects in Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/json\\\/parse-json-objects-in-python#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonforbeginners.com\\\/json\\\/parse-json-objects-in-python\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/json\\\/parse-json-objects-in-python#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Parse JSON objects in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\",\"name\":\"PythonForBeginners.com\",\"description\":\"Learn By Example\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\",\"name\":\"PythonForBeginners.com\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/PFB-Logo-Final.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/PFB-Logo-Final.png\",\"width\":1868,\"height\":318,\"caption\":\"PythonForBeginners.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/pythonbeginners\",\"https:\\\/\\\/x.com\\\/pythonbeginners\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/person\\\/1f49841b9ba76108d92e7ef1ca5ee648\",\"name\":\"PFB Staff Writer\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"caption\":\"PFB Staff Writer\"},\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/author\\\/pfb_staff\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Parse JSON objects in Python - PythonForBeginners.com","description":"Parse JSON objects in Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","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.pythonforbeginners.com\/json\/parse-json-objects-in-python","og_locale":"en_US","og_type":"article","og_title":"Parse JSON objects in Python - PythonForBeginners.com","og_description":"Parse JSON objects in Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","og_url":"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python","og_site_name":"PythonForBeginners.com","article_publisher":"https:\/\/www.facebook.com\/pythonbeginners","article_published_time":"2012-10-06T11:56:28+00:00","article_modified_time":"2020-08-27T14:21:22+00:00","author":"PFB Staff Writer","twitter_card":"summary_large_image","twitter_creator":"@pythonbeginners","twitter_site":"@pythonbeginners","twitter_misc":{"Written by":"PFB Staff Writer","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python#article","isPartOf":{"@id":"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python"},"author":{"name":"PFB Staff Writer","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/person\/1f49841b9ba76108d92e7ef1ca5ee648"},"headline":"Parse JSON objects in Python","datePublished":"2012-10-06T11:56:28+00:00","dateModified":"2020-08-27T14:21:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python"},"wordCount":156,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonforbeginners.com\/#organization"},"articleSection":["Code Snippets","Json","Python On The Web"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python","url":"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python","name":"Parse JSON objects in Python - PythonForBeginners.com","isPartOf":{"@id":"https:\/\/www.pythonforbeginners.com\/#website"},"datePublished":"2012-10-06T11:56:28+00:00","dateModified":"2020-08-27T14:21:22+00:00","description":"Parse JSON objects in Python will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","breadcrumb":{"@id":"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonforbeginners.com\/json\/parse-json-objects-in-python#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonforbeginners.com\/"},{"@type":"ListItem","position":2,"name":"Parse JSON objects in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonforbeginners.com\/#website","url":"https:\/\/www.pythonforbeginners.com\/","name":"PythonForBeginners.com","description":"Learn By Example","publisher":{"@id":"https:\/\/www.pythonforbeginners.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonforbeginners.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonforbeginners.com\/#organization","name":"PythonForBeginners.com","url":"https:\/\/www.pythonforbeginners.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonforbeginners.com\/wp-content\/uploads\/2020\/05\/PFB-Logo-Final.png","contentUrl":"https:\/\/www.pythonforbeginners.com\/wp-content\/uploads\/2020\/05\/PFB-Logo-Final.png","width":1868,"height":318,"caption":"PythonForBeginners.com"},"image":{"@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/pythonbeginners","https:\/\/x.com\/pythonbeginners"]},{"@type":"Person","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/person\/1f49841b9ba76108d92e7ef1ca5ee648","name":"PFB Staff Writer","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","caption":"PFB Staff Writer"},"url":"https:\/\/www.pythonforbeginners.com\/author\/pfb_staff"}]}},"jetpack_publicize_connections":[],"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"PFB Staff Writer","author_link":"https:\/\/www.pythonforbeginners.com\/author\/pfb_staff"},"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/1297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/comments?post=1297"}],"version-history":[{"count":3,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/1297\/revisions"}],"predecessor-version":[{"id":7904,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/1297\/revisions\/7904"}],"wp:attachment":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/media?parent=1297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/categories?post=1297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/tags?post=1297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}