GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • Learn Python
  • Python Tutorial
  • Basics
  • Lists
  • Dictionary
  • Code Snippets
  • Comments
  • Modules
  • API
  • Beautiful Soup
  • Cheatsheet
  • Games
  • Loops

This view

  • Original page
  • New address

GeezerVison is showing www.pythonforbeginners.com — Parse JSON objects in Python - PythonForBeginners.com

You are here: Home / Json / Parse JSON objects in Python

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 the API.

In my example, I will use the Twitter API.

Start with importing the modules that we need for the program.

import json
import urllib2

Open the URL and the screen name.

url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=wordpress"

Print out the result

print data

Using the Twitter API to parse data

This is a very simple program, just to give you an idea of how it works.

#Importing modules
import json
import urllib2

# Open the URL and the screen name
url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=wordpress"

# This takes a python object and dumps it to a string which is a JSON representation of that object
data = json.load(urllib2.urlopen(url))

#print the result
print data

If you are interested to see another example of how to use JSON in Python, please
have a look at the “IMDB Crawler” script.

To use the Twitter API, see the official documentation on Twitter.
https://dev.twitter.com/docs

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files formatting Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scripts Split Strings System & OS urllib2
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.