import json
with open('result.json', 'w') as fp:
    json.dump(sample, fp)

This is an easier way to do it.

In the second line of code the file result.json gets created and opened as the variable fp.

In the third line your dict sample gets written into the result.json!

Answer from moobi on Stack Overflow
🌐
KDnuggets
kdnuggets.com › convert-python-dict-to-json-a-tutorial-for-beginners
Convert Python Dict to JSON: A Tutorial for Beginners - KDnuggets
April 9, 2024 - So each book record is in a Python dictionary with the following keys: title, author, publication_year, and genre. When calling json.dumps(), we set the optional indent parameter—the indentation in the JSON string as it helps improve readability (yes, pretty printing json we are ??):
Discussions

How to append an dictionary into a json file?
Your problem is not appending strings/jsons to a file. Your problem is later on reading it. json files usually hold ONE json object, not many concatenated objects. You can create a list, and have you dictionaries be items in that list, and then append to it, and dump the entire list as JSON to have multiple dictionaries stored, but eventually its one JSON object. If you want to modify/append a json object in a file, you read it first, load/loads, change it however you want, and write it back. # load it with open("index.json" , "r") as json_file: file_data = json.loads(json_file.read()) # change it file_data.append(dict) # write it all back with open("index.json" , "w") as json_file: json_file.write(json.dumps(file_data)) (Obviously above code assumes you already have a list dumped as a json object in index.json) More on reddit.com
🌐 r/learnpython
2
1
September 2, 2023
convert list of dictionary to list of json objects
I might finally learn what goes on in JSON files! ... Save dict{} to a file that can actually be opened and read by a human, but also retrieved as a dict in my code. ... I have some basic python knowledge but its been a long while and I’ve never used it for work. More on reddit.com
🌐 r/learnpython
7
2
October 13, 2022
python - Converting dictionary to JSON - Stack Overflow
The JSON is loaded into a new variable not because it is new information, but to show that the values in the new dictionary, i.e. after the JSON encode and decode round-trip, are identical. 2019-03-22T10:12:06.877Z+00:00 ... you can dump as json file too, like this with open('r.json', 'w') ... More on stackoverflow.com
🌐 stackoverflow.com
python - How can I import the first and only dict out of a top-level array in a json file? - Stack Overflow
I tried to think about how can ... way of importing the file already as a dict? ... Your data get's imported as list, because in your JSON file the main structure is an Array (squared brackets), which is comparable to a list in Python.... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-python-dictionary-to-json
How To Convert Python Dictionary To JSON? - GeeksforGeeks
July 12, 2025 - Note: For more information, refer to Read, Write, and Parse JSON using Python ... json.dump() writes a dictionary directly into a file in JSON format. It avoids the need to manually open and write the JSON string to a file.
🌐
Analytics Vidhya
analyticsvidhya.com › home › how to convert python dictionary to json?
How To Convert Python Dictionary To JSON? - Analytics Vidhya
January 11, 2024 - Convert Python dictionaries to JSON for seamless data interchange. Use json module, methods, examples, and best practices and more.
🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
Python has a built-in package called json, which can be used to work with JSON data. ... If you have a JSON string, you can parse it by using the json.loads() method. The result will be a Python dictionary.
🌐
Oregon State University
blogs.oregonstate.edu › logicbot › 2022 › 04 › 08 › saving-a-dictionary-with-json
Saving a dictionary with JSON – Logic_bot
I have to say that I am a little ... with it. Useless. That’s where this little one liner comes in. json.loads() turns JSON formatted data back into a dictionary that you can reference normally....
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › convert list of dictionary to list of json objects
r/learnpython on Reddit: convert list of dictionary to list of json objects
October 13, 2022 - Iterate through dictionary and convert each dict to json object gives ['{"a":"b"}','{"c":"d"}'] Share
🌐
Scaler
scaler.com › home › topics › convert dictionary to json python
Convert Dictionary to JSON Python - Scaler Topics
April 21, 2024 - In the below article, we shall be understanding how we can make use of json.dumps() function by utilizing its various parameters to convert the dict to json python.
🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.3 documentation
February 23, 2026 - Serialize obj to a JSON formatted str using this conversion table. The arguments have the same meaning as in dump(). ... Keys in key/value pairs of JSON are always of the type str. When a dictionary is converted into JSON, all the keys of the dictionary are coerced to strings...
🌐
Medium
medium.com › @techclaw › python-dict-to-json-simplifying-data-serialization-81a8d4eeb512
Python Dict to JSON: Simplifying Data Serialization | by TechClaw | Medium
October 12, 2023 - Converting Python dictionaries to JSON becomes necessary when you need to: Communicate with web services that require data in JSON format. Save data to a file for later use.
🌐
Python Examples
pythonexamples.org › python-dict-to-json
Python Dictionary to JSON
Python Convert Dictionary to JSON - To convert a Dict to JSON in Python, you can use json.dumps() function. json.dumps() function converts the Dictionary object into JSON string.
🌐
Code Beautify
codebeautify.org › blog › convert-python-dictionary-to-json
Convert Python Dictionary to Json
January 20, 2023 - The json.dump() function writes the contents of a Python dictionary to a file-like object in JSON format.
🌐
Replit
replit.com › home › discover › how to convert a dict to json in python
How to convert a dict to JSON in Python | Replit
March 3, 2026 - Set ensure_ascii=False in json.dumps() to stop Python from escaping non-ASCII characters into sequences like \u00e9. Specify encoding='utf-8' when opening the file to ensure it can correctly write those characters. Moving beyond the technical details and error fixes, you'll find dictionary-to-JSON ...
🌐
Python Forum
python-forum.io › thread-35245.html
Write a dictionary with arrays as values into JSON format
Hi I'm working with dictionaries where values are (numpy) arrays, and I'm trying to figure out how to export these dictionaries in an JSON format, and how to read it afterward. I'm not familiar with JSON specifications and that's why I'm facing som...