Json Category Page - PythonForBeginners.com https://www.pythonforbeginners.com Learn By Example Thu, 27 Aug 2020 14:26:33 +0000 en-US hourly 1 https://wordpress.org/?v=5.8.13 https://www.pythonforbeginners.com/wp-content/uploads/2020/05/cropped-pfb_icon-32x32.png Json Category Page - PythonForBeginners.com https://www.pythonforbeginners.com 32 32 201782279 Encoding JSON with Python https://www.pythonforbeginners.com/json/parsing-json-python Mon, 14 Sep 2015 11:31:43 +0000 https://www.pythonforbeginners.com/?p=6743 Python comes pre-equipped with a JSON encoder and decoder to make it very simple to play nice with JSON in your applications The simplest way to encode JSON is with a dictionary. This basic dictionary holds random values of various datatypes. data = { a: 0, b: 9.6, c: "Hello World", d: { a: 4 […]

The post Encoding JSON with Python appeared first on PythonForBeginners.com.

]]>
Python comes pre-equipped with a JSON encoder and decoder to make it very simple to play nice with JSON in your applications

The simplest way to encode JSON is with a dictionary. This basic dictionary holds random values of various datatypes.


data = {
    a: 0,
    b: 9.6,
    c: "Hello World",
    d: {
        a: 4
    }
}

We then use json.dumps() to convert the dictionary to a JSON object.


import json

data = {
    a: 0,
    b: 9.6,
    c: "Hello World",
    d: {
        a: 4
    }
}

json_data = json.dumps(data)
print(json_data)

This will print out


{"c": "Hello World", "b": 9.6, "d": {"e": [89, 90]}, "a": 0}

Notice how the keys are not sorted by default, you would have to add the sort_keys=True argument to json.dumps() like so.


import json

data = {
    a: 0,
    b: 9.6,
    c: "Hello World",
    d: {
        a: 4
    }
}

json_data = json.dumps(data, sort_keys=True)
print(json_data)

Which would then output the sorted keys.


{"a": 0, "b": 9.6, "c": "Hello World", "d": {"e": [89, 90]}}

The post Encoding JSON with Python appeared first on PythonForBeginners.com.

]]>
6743
Python API and JSON https://www.pythonforbeginners.com/json/python-api-and-json Fri, 14 Dec 2012 05:38:17 +0000 https://www.pythonforbeginners.com/?p=2107 What is an API? An application programming interface (API) is a protocol intended to be used as an interface by software components to communicate with each other. It’s basically a set of programming instructions and standards for accessing a Web-based software application or Web tool. A software company (like Amazon, Google etc) releases its API […]

The post Python API and JSON appeared first on PythonForBeginners.com.

]]>
What is an API?

An application programming interface (API) is a protocol intended to be used as an interface by software components to communicate with each other. It’s basically a set of programming instructions and standards for accessing a Web-based software application or Web tool. A software company (like Amazon, Google etc) releases its API to the public so that other software developers can design products that are powered by its service. For a more extended explanation on API, read this excellent article from howstuffworks.com.

Interact with an API using JSON

It is important to know that an API is a software-to-software interface, not a user interface. With APIs, applications talk to each other without any user knowledge or intervention. When we want to interact with an API in Python (like accessing web services), we get the responses in a form called JSON. To interact with JSON, we can use the json and simplejson modules. JSON (JavaScript Object Notation) is a compact, text based format for computers to exchange data and is once loaded into Python just like a dictionary. JSON data structures map directly to Python data types, which makes this a powerful tool for directly accessing data without having to write any XML parsing code.

How do I do this?

Let’s show how we can do this by using Twittes API. The first thing you have to do, is to find an URL to call the API. The next step is to import the modules that we need.

import json
import urllib2

# open the url and the screen name 
# (The screen name is the screen name of the user for whom to return results for)
url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=python"

# 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

More Examples

Using the Youtube API

Using the Vimeo API

Using the Twitter API

Using the Delicious API

LastFM API

Amazon API

Google API

Sources

http://money.howstuffworks.com/business-communications/how-to-leverage-an-api-for-conferencing1.htm

The post Python API and JSON appeared first on PythonForBeginners.com.

]]>
2107
Parsing JSON in Python https://www.pythonforbeginners.com/json/parsingjson Mon, 08 Oct 2012 07:29:40 +0000 https://www.pythonforbeginners.com/?p=1342 Overview Request to an HTTP API is often just the URL with some query parameters. API Response The responses that we get from an API is data, that data can come in various formats, with the most popular being XML and JSON. Many HTTP APIs support multiple response formats, so that developers can choose the […]

The post Parsing JSON in Python appeared first on PythonForBeginners.com.

]]>
Overview

Request to an HTTP API is often just the URL with some query parameters.

API Response

The responses that we get from an API is data, that data can come in various
formats, with the most popular being XML and JSON.

Many HTTP APIs support multiple response formats, so that developers can choose
the one they’re more comfortable parsing.

Getting Started

To get started, lets create a simple data structure and put in some data.

First we import the json module into our program.

import json

# Create a data structure
data = [ { 'Hola':'Hello', 'Hoi':"Hello", 'noun':"hello" } ]

To print the data to screen, is as simple as:

print 'DATA:', (data)

When we print the Data as above, we will see the following output:

DATA: [{'noun': 'hello', 'Hola': 'Hello', 'Hoi': 'Hello'}]

JSON Functions

When you use JSON in Python, there are different function that we can make use of

Json Dumps

The json.dumps function takes a Python data structure and returns it as a JSON string.

json_encoded = json.dumps(data)

# print to screen

print json_encoded

OUTPUT:

[{"noun": "hello", "Hola": "Hello", "Hoi": "Hello"}]

Json Loads

The json.loads() function takes a JSON string and returns it as a Python data
structure.

decoded_data = json.loads(json_encoded)

# print to screen

print decoded_data

OUTPUT:

[{u'noun': u'hello', u'Hola': u'Hello', u'Hoi': u'Hello’}]

The post Parsing JSON in Python appeared first on PythonForBeginners.com.

]]>
1342
Parse JSON objects in Python https://www.pythonforbeginners.com/json/parse-json-objects-in-python Sat, 06 Oct 2012 11:56:28 +0000 https://www.pythonforbeginners.com/?p=1297 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 post Parse JSON objects in Python appeared first on PythonForBeginners.com.

]]>
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

The post Parse JSON objects in Python appeared first on PythonForBeginners.com.

]]>
1297
What is JSON https://www.pythonforbeginners.com/json/what-is-json Sat, 06 Oct 2012 08:59:55 +0000 https://www.pythonforbeginners.com/?p=1295 What is JSON? JSON (JavaScript Object Notation) is a compact, text based format for computers to exchange data. The official Internet media type for JSON is application/json, and the JSON filename extension is .json JSON is built on two structures: A collection of name/value pairs An ordered list of values. JSON take these forms: objects, […]

The post What is JSON appeared first on PythonForBeginners.com.

]]>
What is JSON?

JSON (JavaScript Object Notation) is a compact, text based format for computers to exchange data. The official Internet media type for JSON is application/json, and the JSON filename extension is .json

JSON is built on two structures:

A collection of name/value pairs
An ordered list of values.

JSON take these forms: objects, array, value, string, number

Object

Unordered set of name/value pairs. Begins with { and ends with }. Each name is followed by : (colon) The name/value pairs are separated by , (comma).

Array

Ordered collection of values. Begins with [ and ends with ]. Values are separated by , (comma).

Value

Can be a string in double quotes, number, or true or false or null, or an object or an array.

String

A sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes.

Number

Integer, long, float

The following example shows the JSON representation of an object that describes a person:

{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10021"
    },
    "phoneNumber": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}

JSON data structures map directly to Python data types, so this is a powerful tool for directly accessing data without having to write any XML parsing code. JSON is once loaded into Python just like a dictionary.

The post What is JSON appeared first on PythonForBeginners.com.

]]>
1295