Decode JSON in Python
JSON string decoding is done with the help of the inbuilt method json.loads() & json.load() of JSON library in Python. The json.loads() is used to convert the JSON String document into the Python dictionary, and The json.load() is used to read the JSON document from the file.
Python3
from io import StringIO import json fileObj = StringIO( '["Geeks for Geeks"]' ) print ( "Using json.load(): " + str (json.load(fileObj))) print ( "Using json.loads(): " + str (json.loads ( '{"Geeks": 1, "for": 2, "Geeks": 3}' ))) print ( "Using json.JSONDecoder().decode(): " + str (json.JSONDecoder().decode ( '{"Geeks": 1, "for": 2, "Geeks": 3}' ))) print ( "Using json.JSONDecoder().raw_decode(): " + str (json.JSONDecoder().raw_decode('{ "Geeks" : 1 , "for" : 2 , "Geeks" : 3 }'))) |
Output:
Using json.load(): ['Geeks for Geeks'] Using json.loads(): {'for': 2, 'Geeks': 3} Using json.JSONDecoder().decode(): {'for': 2, 'Geeks': 3} Using json.JSONDecoder().raw_decode(): ({'for': 2, 'Geeks': 3}, 34)
JSON Formatting in Python
JSON (JavaScript Object Notation) is a popular data format that is used for exchanging data between applications. It is a lightweight format that is easy for humans to read and write, and easy for machines to parse and generate.