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
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
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
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
JSON Effectively is a Dictionary? Change My Mind
It’s essentially a mix of dictionaries and lists. A JSON object is similar to a dictionary, and a JSON array is similar to a list. Pretty much any JSON data structure can be translated into a complex Python data object. More on reddit.com
🌐 r/learnpython
7
0
December 17, 2022
🌐
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.
🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.4 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...
Find elsewhere
🌐
Oregon State University
blogs.oregonstate.edu › logicbot › 2022 › 04 › 08 › saving-a-dictionary-with-json
Saving a dictionary with JSON – Logic_bot
April 8, 2022 - 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....
🌐
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.
🌐
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 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.
🌐
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 - 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).
🌐
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.
🌐
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...
🌐
Reddit
reddit.com › r/learnpython › what's the real difference between a dict and a json object (coming from js background)
r/learnpython on Reddit: What's the real difference between a dict and a JSON object (coming from JS background)
October 9, 2022 -

Hello all,

I'm going through 100 Days of Python and reading "Introducing Python" by O'Reilly one thing that bothers me is the dict (insert funny joke). But seriously, what's the difference between a dict and a JSON object in JavaScript? Is there really any difference or should I just treat them as the same?

Kind regards

Top answer
1 of 4
16
JSON is a file format that uses a key - value pair syntax. Keys can only be unicode strings surrounded by doubel quotes, like "name". Values can be: Only certain number formats like 5, -5, 5.5, 1.0E+5 The values true, false, and null Unicode strings in double quotes, like "hello" Array: an ordered, comma separated list of any valid value type Object: a collection of key-value pairs that itself confirms to the JSON syntax ---------------------------------- A Python dict is an object that can have any hashable object as a key. It’s more flexible. A number, a decimal, a string, a tuple, etc can be a key. A Python dict value has no limits (that I know of). It can be any Python object. A string, a number, a function, another dict, a whole entire module. You name it. ---------------------------------- What makes JSON so powerful is that it's an agreed upon format. No matter if you're using Python, Java, PHP, or Ruby, if someone sends your app data in the JSON format, the shape is predictable so your programming language of choice will be able to parse it. How Java, Python, JavaScript, etc. decide to parse JSON into an object will be a little different depending on the language. Python can decode a string or file that confirms to the JSON format into a Python dict. ---------------------------------- One nuance that I wanted to point out is even though JSON stands for "JavaScript Object Notation", it is NOT JavaScript. JSON is a file format/data exchange format. Someone said "So 1 and 1.0 are two different values in Python, while in JSON, they would be the same." That's not quite true. JSON is only a format. 1 and 1.0 in a json file are no more "the same" as 1 and 1.0 are in a txt file. What that person probably meant is that 1 and 1.0 are the same in JavaScript, but that's besides the point because it's important to remember that JSON is NOT JavaScript, it's a format.
2 of 4
4
The most obvious difference is that JSON objects have to have strings as keys, and JSON values as values (which can only be null, true, false, numbers, strings, arrays or objects). Python dictionaries can have any Python object as either a key or a value. Also they are just different languages and there are subtle differences in the way they model data even if they are roughly equivalent a lot of the time. For example, Python distinguishes ints from floats, whereas in JSON there are only "numbers". So 1 and 1.0 are two different values in Python, while in JSON, they would be the same.