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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-convert-python-dictionary-to-json
How To Convert Python Dictionary To JSON? - GeeksforGeeks
July 12, 2025 - Dictionaries can have nested structures and json.dumps() effectively handles such cases. With indentation, nested objects remain easy to read, ensuring clarity in complex data structures. Encoding and sorting keys can also be applied to nested dictionaries, preserving the hierarchical structure.
Discussions

convert list of dictionary to list of json objects
Json.dumps will convert the dictionary to a string but the string is valid JSON. You see the single quotes since Python is indicating this is a string (and double quotes is being used internally). More on reddit.com
๐ŸŒ r/learnpython
7
2
October 13, 2022
Store data as Python or JSON file?
By "write the dict object in Python file", do you mean generating Python code? Definitely json is the better option. More on reddit.com
๐ŸŒ r/learnpython
8
4
December 12, 2019
In the debugger I often have to convert python dict to json is there a better way than evaluate expression with json.dumps()?
You are leaving out the critical explanation of why you are doing this. First, a Python dictionary is not automatically valid JSON. Hence, the [potential] need to leverage json.dumps. Pycharm supports macros (keystroke injection), but not automation (which is what you are really asking for). You could create your own extension, but that seems overkill. So for simplicity sake, you may want to just create a simple logging method that takes in a dictionary and calls pyperclip.copy(json.dumps(my_dict)). Don't forget to pip pyperclip. More on reddit.com
๐ŸŒ r/pycharm
3
5
March 9, 2023
How would you use python to create JSON files?
I read two concrete questions from your post: why you would want to create a JSON object Imagine you want multiple software systems to communicate. Let's say we have three systems, one written in Python, one client written in JavaScript and one more backend system in Java. You can't just send Python objects over a network and expect the systems written in JS or Java to understand them. Same thing the other way around. In the end it's just electrical signals and both the sender and receiver need a common understanding of how to interpret those signals, otherwise they will just be gibberish. That's where data formats like JSON come into play: It's a simple and standardized data format that can be handled in any modern programming language. Now your Python code can serialize its internal representation of a piece of data into this format and send it over a network or store it on some disk, where some other system will eventually pick it up, deserialize it into its own internal representation and process it. show how you would create a JSON object with python import json data = {"year": 2020, "sales": 12345678, "currency": "โ‚ฌ"} # creating a JSON string json_string = json.dumps(data) # storing it in a file with open("data.json", "w") as json_file: json.dump(data, json_file) More on reddit.com
๐ŸŒ r/learnpython
7
5
March 7, 2020
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to append an dictionary into a json file?
r/learnpython on Reddit: How to append an dictionary into a json file?
September 2, 2023 -

Hello I currently learning json in python i want to append a dictionary in a json file ontop of existing ones but every time i do this i get this error in VS-Code:

End of file expected.

Can somebody help me?

Here is the Code:

dict = {
    "data1" : data3,
       
    "data2" : data4        
}
    
data = json.dumps(dict)
with open("index.json" , "a") as file:
    
    json.dump(data , file)

๐ŸŒ
KDnuggets
kdnuggets.com โ€บ convert-python-dict-to-json-a-tutorial-for-beginners
Convert Python Dict to JSON: A Tutorial for Beginners - KDnuggets
To convert a Python dictionary to JSON string, you can use the dumps() function from the json module. The dumps() function takes in a Python object and returns the JSON string representation.
๐ŸŒ
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.
๐ŸŒ
Oregon State University
blogs.oregonstate.edu โ€บ logicbot โ€บ 2022 โ€บ 04 โ€บ 08 โ€บ saving-a-dictionary-with-json
Saving a dictionary with JSON โ€“ Logic_bot
In order to save anything into a JSON file you need to covert it into the proper format. This is where json.dumps() function comes in. You can see below that I declared and example dictionary and then converted the dictionary by passing it to json.dumps()
Find elsewhere
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ convert python dictionary to json
Convert Python Dictionary to JSON - Spark By {Examples}
May 31, 2024 - We can use the json.dump() method and json.dumps() method to convert a dictionary to a JSON string. ... JSON stands for JavaScript Object Notation and is used to store and transfer data in the form of text.
๐ŸŒ
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...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ write json data to a file in python
Write JSON data to a file in Python | Sentry
We can do this using Pythonโ€™s built-in json library and file operations. Specifically, the json.dump function allows us to serialize a Python dictionary as JSON for writing to disk.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ convert dictionary to json python
Convert Dictionary to JSON Python - Scaler Topics
April 21, 2024 - A nested dictionary in python can be created by simply declaring new dictionaries in the default dictionary. To convert dict to JSON python, we can use the dumps() function itself.
๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ python-create-json-file-from-dict-exampleexample.html
Python Create JSON File from Dict Example - ItSolutionstuff.com
October 30, 2023 - You can use these examples with python3 (Python 3) version. ... import json # Data to be written in dict dictionary = { "id" : 1, "name" : "Hardik Savani", "email" : "hardik@gmail.com", "city" : "Rajkot" } with open("data.json", "w") as outfile: json.dump(dictionary, outfile) print("New data.json file is created from dict")
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_json.asp
Python JSON
If you have a JSON string, you can parse it by using the json.loads() method. The result will be a Python dictionary.
๐ŸŒ
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. Alternatively, you can use the json.dumps() function to convert the dictionary to a JSON string and then write the string to a file: In ...
๐ŸŒ
Real Python
realpython.com โ€บ python-json
Working With JSON Data in Python โ€“ Real Python
August 20, 2025 - You can write JSON with Python by using the json.dump() function to serialize Python objects into a JSON file. ... You connect JSON with Python by using the json module to serialize Python objects into JSON and deserialize JSON data into Python ...
๐ŸŒ
Edureka Community
edureka.co โ€บ home โ€บ community โ€บ categories โ€บ python โ€บ is it possible to save python dictionary into...
Is it possible to save python dictionary into json files | Edureka Community
September 4, 2019 - 56319/is-it-possible-to-save-python-dictionary-into-json-files ... Is it possible to save python dictionary into... Reading different format files from s3 having decoding issues using boto3 May 17, 2024
๐ŸŒ
Quora
quora.com โ€บ How-can-we-convert-a-Python-nested-dictionary-to-a-JSON-file
How can we convert a Python nested dictionary to a JSON file? - Quora
To convert a Python nested dictionary to a JSON file, use the built-in json module to serialize the dict and write it to disk. Key points: ensure all values are JSON-serializable (convert non-serializable types like datetime, set, bytes), choose ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-write-multiple-Python-dictionaries-to-a-JSON-file
How to write multiple Python dictionaries to a JSON file - Quora
First load the json file with an empty Dict. with open(โ€˜file.json', โ€˜w') as f: json.loads(โ€œ{}โ€,f) Then write the Dict and store the data. Open the json file in read mode. with open(โ€˜file.json', โ€˜r') as r: Data = hain.dumps(r) ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-list-of-dictionaries-to-json
Python - Convert list of dictionaries to JSON - GeeksforGeeks
July 5, 2025 - json.dumps() converts the list of dictionaries into a JSON string, using a default lambda to transform tuples into lists for compatibility.