Python | Dict to JSON

When you are working with APIs or trying to scrape a website, you sometimes need to process data in JSON format. In Python, we have something really similar to JSON objects, which are called dictionaries. These dictionaries are comparable to JSON objects in that they are both used to store data in key-value pairs. However, a computer will process these objects differently. In this article, I will explain what a Python dictionary is, how it is different from a JSON object, how you can convert a Python dict to JSON, and vice-versa how to convert JSON to Python dict.

python dict to JSON header

Python Dictionary

When you think about an actual real-life dictionary you already know that you can find the meaning of a word by searching for the word itself. That is also how a dictionary in Python works; you can find the value of a certain item by looking for the key. The analogy does not stop there, as Python dictionaries are also ordered. Additionally, when you create an item in a dictionary in Python 3.7, you can find it by searching for the index. Note that in previous Python versions, a dictionary was considered unordered. You can create a dictionary with curly brackets like so:

automation_help_dict = {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
}

Dictionaries in Python are changeable. So even after having defined your variable like in the above-mentioned code, you can change one value like so:

automation_help_dict["key1"] = "value1_new"

Python JSON

You can handle JSON objects in Python with the built-in package called JSON. The name JSON is an abbreviation of JavaScript object notation. When working with any JSON data in Python you will have to import this package.

import json

Like I mentioned before, a JSON object looks really similar to a Python dictionary (key-value) pairs. However, this is because we are looking at a text representation of the actual object. When a computer processes these objects, it is looking at different bytes (0s and 1s). Therefore, sometimes you will need to convert these objects to continue processing your data.

Convert Dict to JSON

In Python to convert a dictionary to JSON, you can use the json.dumps function.

import json
automation_help_dict = {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
}
# Convert Dict to JSON:
automation_help_json = json.dumps(automation_help_dict)

You should know that this function allows you to convert many types of Python object into a JSON object. For example list, tuples, str, integer, floats, true, false, None.

Convert JSON to Dict

The other way around, you can convert JSON objects to Dictionaries with the function json.loads.

import json
automation_help_json = '{ "key1":"value1", "key2":value2, "key3":"value3"}'
# Convert JSON to Dict:
y = json.loads(automation_help_json)

I hope you understand how to convert JSON objects to dictionaries, and vice-versa convert dictionaries to JSON objects. With this basic knowledge, it will be easier for you to follow any of my more advanced guides such as Creating a simple web scraper. If you are still new to coding, and you have not even been able to properly install Python, please check out my guide with an easy way of installing using Anaconda. Do not hesitate to leave a comment if you have any questions!

1 thought on “Python | Dict to JSON”

  1. Pingback: Python Requests Library (HTTP requests) - Automation Help

Leave a Comment

Your email address will not be published. Required fields are marked *