Skip to Main Content
It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.

File Conversion

This guide helps users convert to / from different file formats for your project needs.

JSON to Python

JSON to Python (Reading In JSON Files)

  1. We use Python’s built-in JSON module with JSON files. Import the JSON module using the following import statement:

                     

  1. Use one of the two methods from the JSON module to read in JSON data in Python.

The loads method parses a string of JSON code and turns it into a Python dictionary

                               jsonstring2dict = json.loads(‘jsonstring’)

                        The load method translates the data in a JSON file into a Python dictionary.

                               with open(‘jsonfilename.json’, ‘r’) as f:

                  json2dict = json.load(f)

EXAMPLE 1 (loads Method)

          We have a JSON string we are attempting to read in and convert to a Python Dictionary.

                     

           Output (a Python Dictionary):  

                     

 
EXAMPLE 2 (load Method)

           Here, we have a JSON file we wish to convert into a Python dictionary. 

           Download sample.json

                    

 

There are two steps to read in a JSON file: 

  1.  Open the JSON file
  2. Load the file using the load method 

           In Python, we can do both of these steps at once like this: 

              

           Output (Python Dictionary):