How to return a JSON object from a Python function in Tkinter?

PythonTkinterGUI-ProgrammingJSON

JSON is a JavaScript Object Notation file that contains information of a particular data in the form of a dictionary. The pack of data returned by the JSON file can be used further by other languages for different purposes.

Python has a rich library for creating and developing applications to deal with JSON data. We need to import the json package from the Python library.

In this article, we will create a Python function that will return a JSON object. JSON is a built-in library that ships with the Python packages. However, to install JSON, we can simply type “pip install json”.

For this example, we will create a Python function that contains key-value pairs and using that, we will create a dictionary. After creating the dictionary, we have to pass it in the dump(dictionary) method as a parameter where it will return a JSON object.

Example

#Import JSON Library
import json

#Create and define the function

def fun():
   title="Python"
   Tag= "Programming Language"
   Category= "Open Source"
   Downloads= "4500005230"

   #Let us create the dictionary for the above data

   dictionary={
      "title": title,
      "Tag": Tag,
      "Category": Category,
      "Downloads": Downloads
   }
   return json.dumps(dictionary)

print(fun())

Output

Running the above code will simply return the JSON object from the function.

{"title": "Python", "Tag": "Programming Language", "Category": "Open Source", "Downloads": "4500005230"}
raja
Published on 06-Mar-2021 08:37:55
Advertisements