JSON in Python
About JSON
JSON (JavaScript Object Notation) is a lightweight data interchange format.
JSON is used to store and exchange information between a server and a client, or between different systems.
JSON data is easy for humans to read and write, and easy for machines to parse and generate.
JSON is widely used because it is language-independent.
Basic Structure of JSON
JSON data is written as name-value pair and enclosed in curly braces '{}'.
Name and value are paired by colon ':'. (e.g. {"name": "John"})
The name-value pairs are separated by comma (,). (e.g. {"name": "John", "age": 30})
Name must be string type and is always enclosed in double quotes.
Value must be of a specific type. (e.g. {"age": 30})
Example of JSON Structure
Explanation of JSON Structure Example
-
"firstname": "John": Here, "firstname" is the name field enclosed in double quotes and "John" is value of name field and it is string type data. They are paired by colon (:). -
"age": 30: Here, "age" is the name field and 30 is the value of name field and it is an integer (Number) type data. -
"city": "New York": Here, "city" is the name field and "New York" is the value of name field and it is a string type data. -
"hobbies": ["reading", "coding", "gaming"]: Here, "hobbies" is the name field and ["reading", "coding", "gaming"] is the value of name field and it is an array of string. -
"isStudent": true: Here, "isStudent" is the name field and true is the value of name field and it is a Boolean type data.
JSON Data Types
JSON supports the following data types:
-
String: String is a sequence of characters enclosed in double quotes (""). Single quotes are not allowed. Example:{ "name": "John Doe" }("John Doe" is a string) -
Number: Number represents numeric values, including integers and floating-point numbers. Example:{ "age": 30 }(30 is a number) -
Boolean: Boolean represents a logical value: true or false. Example:{ "isStudent": true }(true is boolean) -
Null: Null represents an empty or undefined value. Example:{ "designation": null } -
Object: Object represents a collection of key-value pairs enclosed in curly braces{}. Example:{ "person": {"name": "John", "age": 30} }( {"name": "John", "age": 30} is an object) -
Array: Array represents an ordered list of values enclosed in square brackets[]. Example:{ "hobbies": ["reading", "coding", "gaming"] }(["reading", "coding", "gaming"] is an array)
Python 'json' Module
Python has a built-in json module to work with JSON data. The json module provides functions to parse JSON strings and convert Python objects to JSON format.
Python json module also provides functions to read JSON data from files and write JSON data to files.
Main functions of the json module are:
-
json.loads(): This function is used to parse a JSON string and convert it into a Python dictionary or list. -
json.dumps(): This function is used to convert a Python object (dictionary, list, etc.) into a JSON string. -
json.load(): This function is used to read JSON data from a file and convert it into a Python object. -
json.dump(): This function is used to write a Python object to a file in JSON format.
Convert Python Objects to JSON
You can convert Python objects such as dictionaries, lists, tuples, strings, numbers, and booleans to JSON format using the json.dumps() function from the json module.
Example of Converting Python Dictionary to JSON Object
Example of Converting Python Dictionary to JSON Object with Indentation
Example of Converting Python List to JSON Array
Convert JSON to Python Objects
You can convert JSON data to Python objects such as dictionaries and lists using the json.loads() function from the json module.
Example of Converting JSON Object to Python Dictionary
Example of Converting JSON Array to Python List
Reading JSON Data from Files
You can read JSON data from a file and convert it into a Python object using the json.load() function from the json module.
Please try the following examples in VS Code on your PC as our online server environment does not support file operations.
Example of Reading JSON Data from a File
Writing JSON Data to Files
You can write Python objects to a file in JSON format using the json.dump() function from the json module.
Please try the following example in VS Code on your PC as our online server environment does not support file operations.
Example of Writing JSON Data to a File
Python Compiler For Live Practice
Practice the above Python code in the following Python Editor. Convert more JSON data to Python object and Python object to JSON data to test your understanding in the following code editor and click Execute button. Output will be displayed in Python Code Output section.
Python Editor