JSON Formatting in Python Last Updated : 23 Aug, 2023 Comments Improve Suggest changes Like Article Like Report 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. Python Format JSON Javascript Object Notation abbreviated as JSON is a lightweight data interchange format. It encodes Python objects as JSON strings and decodes 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, you need to format the JSON output to render the necessary results. So let's dive into the JSON module which Python offers for formatting JSON output.Python JSON Functionsjson.dump(obj, fileObj): Serializes obj as a JSON formatted stream to fileObj.json.dumps(obj): Serializes obj as JSON formatted string.json.load(JSONfile): De-serializes JSONfile to a Python object.json.loads(JSONfile): De-serializes JSONfile(type: string) to a Python object.Python JSON ClassesJSONEncoder: An encoder class to convert Python objects to JSON format.JSONDecoder: A decoder class to convert JSON format files into Python obj. The conversions are based on this conversion table. Python JSON encoding The JSON module provides the following two methods to encode Python objects into JSON format. We will be using dump(), dumps(), and JSON.Encoder class. The json.dump() method is used to write Python serialized objects as JSON formatted data into a file. The JSON. dumps() method encodes any Python object into JSON formatted String. Python3 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['""']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) Comment More infoAdvertise with us Next Article JSON Formatting in Python kartik Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python - Output Formatting In Python, output formatting refers to the way data is presented when printed or logged. Proper formatting makes information more understandable and actionable. Python provides several ways to format strings effectively, ranging from old-style formatting to the newer f-string approach.Formatting Out 5 min read Formatting Cells using openpyxl in Python When it comes to managing Excel files programmatically, Python offers a powerful tool in the form of the openpyxl library. This library not only allows us to read and write Excel documents but also provides extensive support for cell formatting. From fonts and colors to alignment and borders, openpy 4 min read Python Modulo String Formatting In Python, a string of required formatting can be achieved by different methods. Some of them are; 1) Using % 2) Using {} 3) Using Template Strings In this article the formatting using % is discussed. The formatting using % is similar to that of 'printf' in C programming language. %d - integer %f - 2 min read Format a Number Width in Python Formatting numbers to a fixed width is a common requirement in various programming tasks, especially when dealing with data presentation or storage. By understanding these techniques, developers can ensure consistent and visually appealing formatting of numerical data in their Python. Format a Numbe 3 min read Python Docstrings When it comes to writing clean, well-documented code, Python developers have a secret weapon at their disposal â docstrings. Docstrings, short for documentation strings, are vital in conveying the purpose and functionality of Python functions, modules, and classes.What are the docstrings in Python?P 10 min read Indentation in Python In Python, indentation is used to define blocks of code. It tells the Python interpreter that a group of statements belongs to a specific block. All statements with the same level of indentation are considered part of the same block. Indentation is achieved using whitespace (spaces or tabs) at the b 2 min read Python JSON Python JSON JavaScript Object Notation is a format for structuring data. It is mainly used for storing and transferring data between the browser and the server. Python too supports JSON with a built-in package called JSON. This package provides all the necessary tools for working with JSON Objects i 3 min read Python print() function The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s 2 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 min read Formatting containers using format() in Python Let us see how to format containers that were accessed through __getitem__ or getattr() using the format() method in Python. Accessing containers that support __getitem__a) For Dictionaries Python3 # creating a dictionary founder = {'Apple': 'Steve Jobs', 'Microsoft': 'Bill Gates'} # formatting prin 1 min read Like