JSON Formatting in Python
Javascript Object Notation abbreviated as JSON is a light-weight data interchange format. It Encode Python objects as JSON strings, and decode JSON strings into Python objects .
- Many of the APIs like Github. send their results in this format. JSON is probably most widely used for communicating between the web server and client in an AJAX application, but is not limited to that problem domain.
- For example, if you are trying to build an exciting project like this, we need to format the JSON output to render necessary results. So lets dive into json module which Python offers for formatting JSON output.
Functions
Classes
The conversions are based on this conversion table.
Implementation
Encoding
We will be using dump(), dumps() and JSON.Encoder class.
#Code will run in Python 3 from io import StringIO import json fileObj = StringIO() json.dump(["Hello", "Geeks"], fileObj) print("Using json.dump(): "+str(fileObj.getvalue())) class TypeEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, type): return str(obj) print("Using json.dumps(): "+str(json.dumps(type(str), cls=TypeEncoder))) print("Using json.JSONEncoder().encode"+ str(TypeEncoder().encode(type(list)))) print("Using json.JSONEncoder().iterencode"+ str(list(TypeEncoder().iterencode(type(dict))))) |
Output:
Using json.dump(): ["Hello", "Geeks"] Using json.dumps(): "" Using json.JSONEncoder().encode" " Using json.JSONEncoder().iterencode['" "']
Decoding
We will be using load(), loads() and JSON.Decoder class.
#Code will run in Python 3 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)
Reference:
This article is contributed by Sri Sanketh Uppalapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- String Formatting in Python using %
- Python | Output Formatting
- JSON | modify an array value of a JSON object
- Python | Sort JSON by value
- Working With JSON Data in Python
- Python | Check whether a string is valid json or not
- Python | Ways to convert string to json object
- Formatting integer column of Dataframe in Pandas
- Difference between JSON and XML
- JavaScript JSON
- JSON web token | JWT
- Javascript | JSON PHP
- How to parse JSON in Java
- JavaScript | JSON HTML
- How to receive JSON POST with PHP



