Solution Review: Convert an Object to JSON File
Explore how to convert objects into JSON files by comparing Python and PowerShell methods. Understand using Python's json module and PowerShell's ConvertTo-JSON cmdlet to serialize data efficiently.
We'll cover the following...
We'll cover the following...
Solution
Let’s understand the solution of the challenge in Python and Powershell.
Python
Note: Run the
cat jfile.jsoncommand after hitting the “Run” button to see the output.
import json
age = {
'david': 34,
'prateek': 35,
'singh': 18
}
# Write your code here
## writing the objects to a file serialized in XML format
with open("/usercode/jfile.json", "w") as filehandler:
json.dump(age, filehandler)Solution in Python
Explanation
- To use