Python JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. Python provides a built-in module called json to work with JSON data.


JSON Syntax

JSON data is written in key/value pairs. Keys are strings, and values can be strings, numbers, objects, arrays, true, false, or null.

Tutorials dojo strip

Example of JSON:




Parsing JSON

To parse a JSON string into a Python dictionary, you can use the json.loads() method.

Example:




Converting to JSON

To convert a Python dictionary into a JSON string, use the json.dumps() method.

Example:




Reading JSON from a File

To read JSON data from a file, you can use the json.load() method.

Example:




Writing JSON to a File

To write JSON data to a file, use the json.dump() method.

Example:




Working with Nested JSON

JSON data can be nested, meaning objects can contain other objects or arrays. You can parse and access nested JSON data similarly.

Example:




Python Labs

Tutorials dojo strip