Skip to Main Content

File Conversion

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

JSON Conversion Using Python

What is JSON?

  • JSON stands for JavaScript Object Notation
  • Lightweight data-exchange format
  • Uses JavaScript syntax, but the JSON format is text only
  • Widely used in displayed data on a web page

JSON is very similar to a nested Python dictionary. However, here are some key differences: 

  • JSON uses double quotes (“”) for all strings
  • JSON uses true and false for boolean values instead of True and False
  • JSON uses null instead of None
  • JSON dictionary keys must be of the type String


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):


Python to JSON

Python To JSON (Writing to JSON Files)

Two methods from the JSON module are used for writing JSON data.

  • The dumps method takes a Python dictionary and returns a JSON string.

dict2jsonstring = json.dumps(the_dict)

  • The dump method takes a Python dictionary and dumps it into a JSON file.

with open(‘file_out.json’, ‘w’) as f

     json.dump(output_dict, f)

EXAMPLE 1 (dumps Method)

            We have a Python Dictionary we are attempting to write to a JSON string. 


            Output (a JSON string):



EXAMPLE 2 (load Method)

            Here, we have a Python Dictionary that we wish to write to a JSON file. 


Again, there are 2 steps: 

  1.  Open a new file and tell Python we are writing to this new file with “w”.
  2. Use the dump method to write the dictionary to the new file.

            Output (JSON file):