Python JSON

JSON stands for JavaScript Object Notation. It is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate.

In Python, you can work with JSON data using the built-in json module. This module provides methods for encoding Python data structures into JSON format and decoding JSON data into Python data structures.

Python to JSON (Encoding)

To convert a Python data structure (e.g., a dictionary, list, or tuple) into a JSON-formatted string, you can use the json.dumps() function (short for "dump string").

JSON Python (Decoding)

To convert a JSON string into a Python data structure, you can use the json.loads() function (short for "load string").

Working with JSON files

You can also read and write JSON data to/from files using the json.dump() and json.load() functions:

  • json.dump(data, file_object) writes JSON data to a file.
  • json.load(file_object) reads JSON data from a file.

Data Types After Conversion

When you convert data from Python to JSON using the json.dumps() function, the data types of the variables in the resulting JSON data will be converted to other compatible type.

  • dict to object
  • array to list
  • int / float / double to numbers
  • bool to bool
  • none to null

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which of the following is a best practice in programming?

10 XP~2 min
Exercise 2 of 2Easy

Code that is easy to read and understand is called ___ code.

10 XP~2 min