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


JavaScript Object Notation, or JSON is a simple data format that is used to exchange data between many different languages. It is simple to read for people and simple for computers to parse.

Python reads JSON text as a quoted-string that contains the value in each key-value mapping. Once parsed, it is accessible in Python as a dictionary object. JSON data can be encoded and decoded using the built-in package json in Python. You must first import the json library in order to work with files of the json type.

Purposeco JSON

Python to JSON conversion is done using serialization. The process of converting data into a series of bytes for storage is referred to as serialization.

Different Python-specific objects are converted to a standard JSON-acceptable format because JSON may be read by other languages. for instance, a list and a tuple, which are Python-specific, are changed to arrays when they are stored in JSON format.

The same applies to importing and parsing a JSON object into a Python dict. It is stored as a "dict" because it has a format that is quite similar to a JSON and can contain other data structures like lists, tuples, and other dicts, among others.

To return a JSON object from a Python function you need to use the dumps() function.

Using dumps() function

When we wish to transfer and store Python objects, we typically use the dumps function, and the json package makes this task quick and easy. It is used for parsing, printing, and other functions when the objects must be in string format. The json file is written directly by this method.

Algorithm

Following is an approach to return a json object from a Python function −

  • Import the module

  • Create the function

  • Create the dictionary

  • Dictionary to JSON Object Conversion with the dumps() Method.

  • Return a JSON Object

Note − The dictionary will be converted to a JSON string and saved in a file using json.dump().

Example

Following is an example of the above approach −

import json # Creating the function def sports(): # Defining the Variable name = "Cricket" player = "Sachin Tendulkar" Playerid = 13 score = 49 # Creating the dictionary values = { "name": name, "player": player, "Playerid": Playerid, "score": score } return json.dumps(values) # Calling the function and printing it print('The dictionary is:',sports())

Output

Following is an output of the above code −

The dictionary is: {"name": "Cricket", "player": "Sachin Tendulkar", "Playerid": 13, "score": 49}

Example

Following is an example of the above explained approach using the list as a dictionary value −

import json # Creating the function def sports(): # Creating the dictionary values = { "name": "Cricket", "player": "Hardik Pandya", "position": ["Batsman", "Bowler", "Fielder"], "Playerid": 13, "score": [{ "ODI match": 100, "T-20 match": 89, "Test match": 200 }, { "Asia Cup": 249, "ODI World Cup": 347, "IPL": 150 } ] } return json.dumps(values) # Calling the function and printing it print('The dictionary is:',sports())

Output

Following is an output of the above code −

The dictionary is: {"name": "Cricket", "player": "Hardik Pandya", "position": ["Batsman", "Bowler", "Fielder"], "Playerid": 13, "score": [{"ODI match": 100, "T-20 match": 89, "Test match": 200}, {"Asia Cup": 249, "ODI World Cup": 347, "IPL": 150}]}

Example

With the help of the provided Python dictionary, we can return a JSON object from a Python function as seen below −

import json dictionary = {'name':'Ananya', 'age': 23, 'isEmployed': True } # python dictionary def returnjson(): result = json.dumps(dictionary) print ('The dictionary is:',result) returnjson()

Output

Following is an output of the above code −

The dictionary is: {"name": "Ananya", "age": 23, "isEmployed": true}

Updated on: 23-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements