Converting Between Python Dictionaries and JSON

We will use json.dums() function to convert to json from python dictionary and json.loads() to convert from json to python dictionary.

In this example code we used flower details data in python dictionary and converted it to json.


import json

# flowers in python dictionary
flowers = {
    "rose": {
        "scientific_name": "Rosa",
        "grows_in": ["gardens", "wild"],
        "colors": ["red", "pink", "white"]
    },
    "sunflower": {
        "scientific_name": "Helianthus annuus",
        "grows_in": ["fields", "gardens"],
        "colors": ["yellow"]
    }
}

# Converting Python dictionary to JSON

flowers_json = json.dumps(flowers)
# converted to json
print(flowers_json)



# we can add indent to make it more readable
flowers_json = json.dumps(flowers, indent=4)
print(flowers_json)





# Now convert json to python dictionary
#fowers_json is alreay in json

flowers_dict = json.loads(flowers_json)
#converted to python dictionary
print(flowers_dict)



Exporting Python Dictionaries to JSON Files

we can also export this python dictionary data to json


import json

# flowers in python dictionary
flowers = {
    "rose": {
        "scientific_name": "Rosa",
        "grows_in": ["gardens", "wild"],
        "colors": ["red", "pink", "white"]
    },
    "sunflower": {
        "scientific_name": "Helianthus annuus",
        "grows_in": ["fields", "gardens"],
        "colors": ["yellow"]
    }
}

# Converting Python dictionary to JSON

flowers_json = json.dumps(flowers)

# Exporting Python dictionary to a JSON file
with open("flowers.json", "w") as json_file:
    json.dump(flowers, json_file, indent=4)


Loading JSON Data from Files into Python Dictionaries


# Loading JSON data from a file into Python dictionary
import json
with open("flowers.json", "r") as json_file:
	#convert to python dictionary with json.load()
    loaded_flowers_dictionary = json.load(json_file)

print(loaded_flowers_dictionary)



Related Posts

Create And Train Simple Artificial Neuron with python : Teach a Neuron to Sum two number
July 1, 2024

We will start this example with simplest neuron then train it to sum two number so it will be simple. And best part is for simplicity we will use only python standard library. We will write everything from scratch.. First the basics. Like our biological neuron artificial neuron can learn and change over time. Human […]

How to create, read and write csv file from python
March 17, 2024

First Lets talk about csv Its a plain text format which comes from (Comma Separated Values) Most spreadsheet application supports this. You can open and edit it from excl, Libra office cal , google sheets and other spread sheet application, also supported by most of the programming language. For simplicity and flexibility often used in […]

Display Matplotlib Plots in Pygame: A Step-by-Step Guide
March 16, 2024

Our tasks for this Import necessary module import sys import pygame import matplotlib from pygame.locals import * import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas This code imports a tool from Matplotlib that lets you save plots as image files (like PNG or JPG). Use this for […]