Advertisement
harsh7i

PyJson.py

Jun 20th, 2022 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. import json
  2.  
  3. # Make an empty dictionary
  4. listObj={}
  5.  
  6. # Open the json file
  7. file=open('post.json')
  8.  
  9. # Convert Json to dictionary for python
  10. listObj=json.load(file)
  11.  
  12. # Printing Json Values
  13. print(listObj)
  14. print(type(listObj))
  15.  
  16. """ Updating Json Data"""
  17.  
  18. # Empty space
  19. print("")
  20.  
  21. # Second Python Dictionary To replace
  22. dictionary2={"elon":"musk","energy":"glucose"}
  23.  
  24. # Adding 2nd Dictionary data to the Python dictionary
  25. listObj=dict(listObj)
  26. listObj.update(dictionary2)
  27.  
  28. # Delete Elements from python dictionary
  29. #del listObj["e"]
  30.  
  31. # Printing Updated Python Dictionary
  32. print(listObj)
  33. print(type(listObj))
  34.  
  35. # Open and write file
  36. file_add=open('post.json','w')
  37.  
  38. # Converting Python dictionary to json
  39. json.dump(listObj,file_add)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement