You build the object before encoding it to a JSON string:

import json

data = {}
data['key'] = 'value'
json_data = json.dumps(data)

JSON is a serialization format, textual data representing a structure. It is not, itself, that structure.

Answer from Martijn Pieters on Stack Overflow
🌐
Python Examples
pythonexamples.org › python-create-json
Python Create JSON
In Python, you can create JSON string by simply assigning a valid JSON string literal to a variable, or convert a Python Object to JSON string using json.loads() function.
Discussions

How to create JSON object in Python - Stack Overflow
JSON stands for JavaScript Object ... such a thing as a "JSON object" but there are objects in programming languages like python and javascript that can be built from them. Is this a sting and you want a dict or is this a dict and you want a string?... More on stackoverflow.com
🌐 stackoverflow.com
python - how to generate json string - Stack Overflow
Just append jsonstring (which is not a string but a dictionary) to your list. ... Yes, just append the dictionary to your list, and encode the list. ... At least, I'm assuming you want studentInfo to be a list. Did you mean for it to be one big dictionary instead? ... Can you please update your question with some sample input, and expected output? That'd avoid further speculation. ... Just encode the end result. You are building a Python ... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
python - How to Turn JSON into a String? - Stack Overflow
My hope was that the printed output of the double json.dumps would be what output's message field would be set to, but it added additional escapes. ... But it doesn't have the single escapes I'm looking for. How might I capture the single escapes when attempting to "stringify" this json? More on stackoverflow.com
🌐 stackoverflow.com
How would you use python to create JSON files?
From StackOverflow JSON is basically a way of communicating data to someone, with very specific rules. Using Key Value Pairs and Arrays. JSON is very lightweight and is a nice way to store information. If you request a web-server, most responses will be in JSON format. Example of JSON data (also from StackOverflow): { "first_name": "John", "last_name": "Smith", "address": { "street_address": "21 2nd Street", "city": "New York", "state": "NY", "postal_code": 10021 }, "phone_numbers": [ "212 555-1234", "646 555-4567" ] } Now, to answer your question. How to create JSON files using Python? First, you need valid JSON in your Python code. images = [ { "name": "awesome_image.png", "file_size": 1240000, "file_type": "image/png", }, { "name": "good_image.jpg", "file_size": 742600, "file_type": "image/jpeg" } ] Now, using the json library # Load data. This will transform the data into an indented string. json_data = json.dumps(images, indent=2) # Create and write to file. with open("/home/user/Documents/images.json", "w") as json_file: json_file.write(json_data) How to load JSON files in Python? # Open file in read mode. with open("/home/user/Documents/images.json", "r") as f: # We can do this in two ways. # 1. Load file directly. data = json.load(f) # 2. Load from the file's contents. data = json.loads(f.read()) Now all the file content is stored in the data variable. If you are dealing with files, it's better to load and work with them in Python. It'll be faster than to keep opening and closing files all the time. Create functions to write/load files and use them when you need to. More on reddit.com
🌐 r/learnpython
7
5
March 7, 2020
🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
If you have a Python object, you can convert it into a JSON string by using the json.dumps() method.
🌐
DataCamp
datacamp.com › tutorial › json-data-python
Python JSON Data: A Guide With Examples | DataCamp
December 3, 2024 - This function is used to serialize a Python object into a JSON string. The dumps() function takes a single argument, the Python object, and returns a JSON string.
🌐
LearnPython.com
learnpython.com › blog › json-in-python
How to Convert a String to JSON in Python | LearnPython.com
The dumps() method converts a Python object to a JSON formatted string. We can also create a JSON file from data stored in a Python dictionary.
🌐
MojoAuth
mojoauth.com › parse-and-generate-formats › parse-and-generate-json-with-python
Parse and Generate JSON with Python | Parse and Generate Formats
November 16, 2025 - Always validate your JSON input before attempting to parse it to avoid unexpected errors. You can easily convert Python objects, such as dictionaries and lists, into JSON formatted strings using the json.dumps() function.
Find elsewhere
🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.7 documentation
The RFC requires that JSON be represented using either UTF-8, UTF-16, or UTF-32, with UTF-8 being the recommended default for maximum interoperability. As permitted, though not required, by the RFC, this module’s serializer sets ensure_ascii=True by default, thus escaping the output so that the resulting strings only contain printable ASCII characters. Other than the ensure_ascii parameter, this module is defined strictly in terms of conversion between Python objects and Unicode strings, and thus does not otherwise directly address the issue of character encodings.
🌐
freeCodeCamp
freecodecamp.org › news › python-json-how-to-convert-a-string-to-json
Python JSON – How to Convert a String to JSON
November 9, 2021 - This comes built-in to Python and is part of the standard library. So, say you have a file named demo.py. At the top you would add the following line: ... #include json library import json #json string data employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}' #check data type with type() method print(type(employee_string)) #output #<class 'str'>
🌐
TutorialsPoint
tutorialspoint.com › how-to-generate-json-output-using-python
How to generate JSON output using Python?
March 5, 2020 - The json module in python allows you to dump a dict to json format directly. To use it, Example import json my_dict = { 'foo': 42, 'bar':
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
July 21, 2026 - Using double quotes is something you can get used to as a Python developer. Comments can be helpful in explaining your code, and trailing commas can make moving lines around in your code less fragile. This is why some developers like to use Human JSON (Hjson) or JSON with comments (JSONC). Hjson gives you the freedom to use comments, ditch commas between properties, or create quoteless strings.
🌐
GeeksforGeeks
geeksforgeeks.org › python-ways-to-convert-string-to-json-object
Convert String to JSON Object - Python - GeeksforGeeks
We have a list of tuples and our task is to convert the list of tuples into a JSON string in Python. In this article, we will see how we can convert a list of tuples to a JSON string in Python. Convert List Of Tuples To Json String in PythonBelow, are the methods of Convert List Of Tuples To Json St · 3 min read Python To Generate Dynamic Nested Json String
Published: April 15, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-to-generate-dynamic-nested-json-string
Python To Generate Dynamic Nested Json String - GeeksforGeeks
July 23, 2025 - In this example, The code defines a Python dictionary `data_dict` representing nested data with attributes like name, age, address, and contacts. It then uses the `json. dumps()` function to convert the dictionary into a formatted JSON string ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › build-a-json-object-in-python
Build a Json Object in Python - GeeksforGeeks
July 23, 2025 - JSON module is imported to deal with JSON objects. A Python dictionary named 'data' is used to store the object in key-value pairs. json.dumps( ) is used convert the Python dictionary into JSON formatted string and result is displayed.
🌐
Esri Community
community.esri.com › t5 › python-questions › python-to-generate-dynamic-nested-json-string › td-p › 257817
Solved: Python to Generate Dynamic Nested JSON String - Esri Community
December 11, 2021 - Greetings, Using python and ArcPy search cursors, I've extracted list(s) of dictionaries containing normalized key value pairs originating from specific tables but residing in a denormalized database layer. In the same script, I am now creating a JSON string with an object containing field & value pair arrays [] that that are to contain the keys and values (key value pairs) I've already distilled into lists of dictionary structures.
🌐
Programiz
programiz.com › python-programming › json
Python JSON: Read, Write, Parse JSON (With Examples)
If you do not know how to read and write files in Python, we recommend you to check Python File I/O. You can convert a dictionary to JSON string using json.dumps() method.
🌐
iProyal
iproyal.com › blog › python-string-to-json
How to Convert a Python String to JSON (Beginner’s Guide)
August 18, 2025 - You create a valid JSON string that mimics a typical JSON object. You use json.loads() to parse it into a Python dictionary.
🌐
CodeSignal
codesignal.com › learn › courses › hierarchical-and-structured-data-formats › lessons › creating-json-like-structures-with-python-dictionaries-and-lists
Creating JSON-like Structures with Python Dictionaries and ...
Dictionaries: A Python dictionary is a collection of key-value pairs. Each key is unique, and its associated value can be accessed using the key. Lists: A list is a collection of ordered items, which can be of any data type. Lists allow you to store multiple items in a single variable. These structures form the backbone of JSON-like representations in Python.