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

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
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
python - Writing a JSON file from dictionary, correcting the output - Stack Overflow
You don't need to use jsonpickle to encode dict data. The json.dump is a wrapper function that convert data to json format firstly, then write these string data to your file. The reason why you found \\ exist between each string is that, jsonpickle have took your data to string, after which the quote(") would convert to Escape character when json... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
๐ŸŒ
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.
๐ŸŒ
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)

๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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")
๐ŸŒ
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...
๐ŸŒ
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()
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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) ...
๐ŸŒ
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
๐ŸŒ
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.