Skip to content

Commit b8f4afa

Browse files
committed
Python JSON
1 parent 9820efa commit b8f4afa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Programs/P51_PythonJSON.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Author: OMKAR PATHAK
2+
# This example shows how to use Python with JSON
3+
4+
import json
5+
6+
# For storing on json format
7+
def storeJSON(fileName, data = {}):
8+
with open(fileName, 'w') as fd:
9+
json.dump(data, fd, indent = 4, separators = (',', ': '))
10+
11+
# For loading data from a JSON file
12+
def loadJSON(fileName):
13+
with open(fileName) as fd:
14+
data = json.load(fd)
15+
print(data)
16+
return data
17+
18+
if __name__ == '__main__':
19+
data = loadJSON('example.json')
20+
print(data['menu']['value']) # File
21+
data['menu']['value'] = 'movie'
22+
storeJSON('example.json', data)
23+
print()
24+
loadJSON('example.json')
25+
print(data['menu']['value']) # movie

0 commit comments

Comments
 (0)