Python JSON Module














































Python JSON Module



Introduction

JSON or JavaScript Object Notation is a file format to store data in human readable format. It stores data as key value pair, where the key needs to be unique but the value can of any type which json can serialize. They popularity of JSON arises from the fact that it replicates the dictionary structure which is very common in most languages like python. The json module comes inbuilt in python and can be imported as - 


import json

#or

from json import *



Functions


The JSON module in python has only two major functions (4 in total but functions like dump/dumps and load/loads show similar behaviour)


1. Dumps - It serializes a dictionary object into a json style string.

2. Loads - It deserializes a json type string into file to a dictionary object.



Dumps

Syntax 

dumps(dict_obj)


Example -

import json

handle = open('data.json','w')

data = {

"hello":123,

456:"world",

"welcome":

"cppsecrets.com",

12:43

}

jsonString = json.dumps(data)

handle.write(jsonString)

handle.close()


Output -



Loads

Syntax 

loads(json_string)


Example -

import json

with open('data.json','r') as handle:

jsonString = handle.read()

jsonData = json.loads(jsonString)

print(jsonData)


Output -


 


.


More Articles of Deekshant Wadhwa:

Name Views Likes
Python Secrets token_urlsafe 1375 1
Python Bisect Module 527 1
Python Base64 Decodebytes 466 1
Python Copy copy 428 1
Python JSON Module 431 1
Python Copy Module 408 1
Python Base64 Encode 527 1
Python Base64 urlsafe_b64decode 571 1
Python Copy deepcopy 440 1
Python 3.9 New removeprefix() and removesuffix() string methods 952 1
Python 3.9 Dictionary Merge & Update Operators 648 1
Python Base64 Decode 441 1
Python Secrets compare_digest 1732 1
Python Itertools Chain 544 1
Python Itertools Permutations 1242 1
Python Itertools Module 425 1
Python Base64 b85decode 619 1
Python Secrets token_bytes 789 1
Python ModuleNotFoundError: No module named cv2 744 1
Python Base64 standard_b64encode 497 1
Python Itertools Count 688 1
Python Secrets Module 669 1
Python Itertools Groupby 446 1
Python Base64 b32decode 709 1
Python Itertools Filterfalse 677 1
Python Itertools Product 471 1
Python Base64 Encodebytes 497 1
Python Itertools Cycle 474 2
Python Base64 b64decode 459 1
Python Itertools Starmap 600 1
Python Itertools combinations_with_replacement 502 1
Python Base64 urlsafe_b64encode 564 1
Python Itertools Repeat 507 1
Python Base64 standard_b64decode 478 1
Python Base64 b85encode 479 1
Python Itertools Islice 574 1
Python Bisect insort 585 1
Python Itertools Combinations 670 1
Python Secrets token_hex 792 1
Python Itertools Compress 494 1
Python Itertools Tee 527 1
Python Base64 b64encode 423 1
Python Itertools zip_longest 922 1
Python Itertools Accumulate 1452 1
Python Itertools Dropwhile 543 1
Python Secrets Randbits 465 1
Python Base64 Module 432 1
Python Itertools combinations_with_replacement 371 10
Python Secrets Choice 450 1
Python Secrets Randbelow 438 1
Python Itertools Takewhile 498 1
Python Bisect bisect 376 1
Python Base64 b32encode 557 1

Comments