Build a Json Object in Python Last Updated : 23 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used for data exchange between a server and a web application, as well as between different components of a system. In Python, working with JSON is straightforward, and the built-in json module provides functions to encode and decode JSON data. In this article, we'll explore how to create and build JSON objects in Python. Build a JSON Object in PythonBelow are some of the ways by which we can build a JSON object in Python: Using Python DictionaryUsing json.loads( )Using json.JSONEncoderBuild a JSON Object Using Python DictionaryJSON module is imported to deal with JSON objects. A Python dictionary named 'data' is used to store the object in key-value pairs. json.dumps( ) is used convert the Python dictionary into JSON formatted string and result is displayed. Python3 import json data = { "name": "Guru", "age": 19, "isStudent": True } json_string = json.dumps(data) print(type(json_string)) print(json_string) Output<class 'str'> {"name": "Guru", "age": 19, "isStudent": true}Build a JSON Object Using json.loads( ):In this example, the json.loads() function is used to parse a JSON-formatted string json_string into a Python dictionary named data. The resulting dictionary is then printed, representing the decoded JSON data. Python3 import json json_string = '{"name": "GFG", "age": 19, "isStudent": true}' data = json.loads(json_string) print(type(json_string)) print(data) Output<class 'str'> {'name': 'GFG', 'age': 19, 'isStudent': True}Build a JSON Object Using json.JSONEncoder: Encoder function is defined that's used for encoding sets and converting to list. A json object named 'gfg' is created as a set. This list is converted into a dictionary named 'json_data'. The dictionary is converted into o a JSON string json_string using json.dumps() with the custom encoder 'Encoder'. Python3 import json def encoder(obj): if isinstance(obj, set): return list(obj) return obj gfg = [('name', 'Hustlers'), ('age', 19), ('is_student', True)] json_data = dict(gfg) json_string = json.dumps(json_data, default=encoder) print(type(json_string)) print(json_string) Output<class 'str'> {"name": "Hustlers", "age": 19, "is_student": true} Comment More infoAdvertise with us Next Article Add Json Library in Python L lakshmi_narasimhan Follow Improve Article Tags : Python Python Programs Python-json Practice Tags : python Similar Reads Check If Python Json Object is Empty Python users frequently work with JSON, particularly when exchanging data between multiple devices. In this article, we'll learn how to check if a JSON object is empty using Python. Check If a JSON Object Is Empty in PythonBelow, we provide examples to illustrate how to check if a JSON object is emp 3 min read Add Json Library in Python Python JSON library is part of the Python standard library, installing it in Python doesn't require installing any other packages. It is a quick and easy process, allowing you to seamlessly work with common data formats. In this article, we will see how to add and use JSON library in Python. Add and 2 min read Python JSON Data Filtering JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used for storing and exchanging data between a server and a web application. Python, with its built-in json module, provides powerful tools for working with JSON data. In this article, we'll explore how to filter and m 3 min read Convert String to JSON Object - Python The goal is to convert a JSON string into a Python dictionary, allowing easy access and manipulation of the data. For example, a JSON string like {"name": "John", "age": 30, "city": "New York"} can be converted into a Python dictionary, {'name': 'John', 'age': 30, 'city': 'New York'}, which allows y 2 min read Modify Json Fields Using Python We have a task to modify JSON fields using Python and print the result. In this article, we will see some generally used methods for modifying JSON fields using Python. Example: Input : '{"name": "Alice", "age": 30, "city": "Los Angeles"}' , age : 31Output : '{"name": "Alice", "age": 31, "city": "Lo 3 min read How to Parse Nested JSON in Python We are given a nested JSON object and our task is to parse it in Python. In this article, we will discuss multiple ways to parse nested JSON in Python using built-in modules and libraries like json, recursion techniques and even pandas.What is Nested JSONNested JSON refers to a JSON object that cont 3 min read Like