Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert Dictionary to JSON in Python

  • 08 Sep, 2025
  • Com 2
How to Convert Dict to JSON in Python

To convert a dictionary to a JSON string in Python, use the built-in json.dumps() method. If you want to write a json file, use the json.dump() method.

Converting dict to json in Python

As you can see from the above picture, the json.dumps() method accepts a dictionary as input and converts it into a JSON-formatted string.

import json

app_dict = {
    'name': 'messenger',
    'playstore': True,
    'company': 'Meta',
    'price': 100
}

print(app_dict)
# Output: {'name': 'messenger', 'playstore': True, 'company': 'Meta', 'price': 100}

print(type(app_dict))
# Output: <class 'dict'>

app_json = json.dumps(app_dict)


print(app_json)
# Output: {"name": "messenger", "playstore": true, "company": "Meta", "price": 100}

print(type(app_json))
# Output: <class 'str'>

If you analyze the output carefully, you can see that the Keys and string values are quoted; numbers and booleans are not.

Using a Nested Dictionary

If the input is a nested dictionary, it will create a json string with the same level of nesting.

import json

nested_app_dict = {
    'app_info': {
        'name': 'messenger',
        'playstore': True
    },
    'company_info': {
        'company_name': 'Meta',
        'price': 100
    }
}

print(nested_app_dict)
# Output:
# {'app_info': {'name': 'messenger', 'playstore': True},
#  'company_info': {'company_name': 'Meta', 'price': 100}}

print(type(nested_app_dict))
# Output: <class 'dict'>

app_json = json.dumps(nested_app_dict, indent=3)

print(app_json)
# Output:
# {
#    "app_info": {
#       "name": "messenger",
#       "playstore": true
#    },
#    "company_info": {
#       "company_name": "Meta",
#       "price": 100
#    }
# }

print(type(app_json))
# Output: <class 'str'>

While using json.dumps(), we passed indent = 3 argument for pretty-print json, which improves the readability of the JSON output, especially for nested structures.

Sorting Keys

To get the consistent order in the JSON output string, you can sort the keys by passing the sort_keys=True argument.
Sorting Keys while converting from dict to json string

 

import json

app_dict = {
    'name': 'messenger',
    'playstore': True,
    'company': 'Meta',
    'price': 100
}

print(app_dict)
# Output: {'name': 'messenger', 'playstore': True, 'company': 'Meta', 'price': 100}

print(type(app_dict))
# Output: <class 'dict'>

app_json = json.dumps(app_dict, sort_keys=True)

print(app_json)
# Output: {"company": "Meta", "name": "messenger", "playstore": true, "price": 100}

print(type(app_json))
# Output: <class 'str'>

Dict to JSON Array

If you want to convert a dictionary to a JSON array, you first need to create a list using list comprehension, where each element is a dictionary containing one key-value pair from the original dictionary, and then use the json.dumps() method to convert it.

import json

app_dict = {
    'name': 'messenger',
    'playstore': True,
    'company': 'Meta',
    'price': 100
}

print(app_dict)
# Output: {'name': 'messenger', 'playstore': True, 'company': 'Meta', 'price': 100}

print(type(app_dict))
# Output: <class 'dict'>

arr = [{x: app_dict[x]} for x in app_dict]

app_json_arr = json.dumps(arr, indent=3)

print(app_json_arr)
# Output:
# [
#    {
#       "name": "messenger"
#    },
#    {
#       "playstore": true
#    },
#    {
#       "company": "Meta"
#    },
#    {
#       "price": 100
#    }
# ]

print(type(app_json_arr))
# Output: <class 'str'>

Writing a dictionary into a file in JSON string

You use the json.dump() method to write the dictionary to the file in JSON format.

The “with statement” context manager ensures the file is properly handled and closed after writing.

import json

app_dict = {
    'name': 'messenger',
    'playstore': True,
    'company': 'Meta',
    'price': 100
}

print(app_dict)
# Output: {'name': 'messenger', 'playstore': True, 'company': 'Meta', 'price': 100}

print(type(app_dict))
# Output: <class 'dict'>

with open('app.json', 'w') as json_file:
    json.dump(app_dict, json_file)

print("Dictionary has been successfully written to JSON File")

# Output: Dictionary has been successfully written to JSON File

Here is the written app.json file:

Writing a dictionary into a file in JSON string in Python

That’s all!

Post Views: 17
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert Binary to Hexadecimal in Python
How to Remove a Property from an Object in JavaScript

2 Comments

  1. Hannah Dean

    August 6, 2020 at 9:04 pm

    AttributeError Traceback (most recent call last)
    In [34]:
    Line 116: jsonData = json.loads(ms_json)

    AttributeError: ‘bytes’ object has no attribute ‘loads’

    is this a common issue

    Reply
  2. Forhave

    December 12, 2022 at 4:41 pm

    This is a great tutorial on how to convert a dictionary to JSON. I have used this technique before to convert a dictionary of user data into a JSON object.

    Reply

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend