A Python JSON Tutorial
The JSON format gives programmers the means to pass data in a standardized format that Python uses to parse key-value pairs. Python includes a JSON parser that splits the keys and values, permitting you to evaluate and display the data. The Python encoder/decoder creates JSON variables and decodes values passed from other pages.
Instructions
-
-
1
Open the Python file in your Python editor. At the beginning of the code, you must import the JSON libraries. Type the following code to import the JSON libraries:
import json
-
2
Load your JSON values. JSON uses an array of key-value pairs. The following code loads a JSON array with two key-value pairs:
array = json.load( { "name": "Joe", "address": "111 Street" } );
-
-
3
Retrieve one of the JSON values. You use the key name in the JSON array to retrieve the value. The following code retrieves the "name" from the JSON array and assigns it to the "name" variable:
name = array['name']
-
4
Print the value to the screen. Typically, you must print the result of the JSON array to the user's screen. The following code prints to the screen:
print name
-
1