
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Represent Python Tuple in JSON Format
In this article, we will demonstrate how to represent a tuple in Python using JSON format. We will explore the following methods throughout the article:
-
Converting Python Tuple to JSON
-
Converting Python Tuple with Different Datatypes to JSON String
-
Parsing JSON string and accessing elements using json.loads() method.
-
Convert the dictionary of tuples to JSON using json.dumps()
What is JSON?
JSON (JavaScript Object Notation) is a lightweight, easy-to-read data-interchange format that both humans and computers can process efficiently. While JSON is derived from JavaScript, it functions as a language-independent text format, making it compatible with Python, Perl, and various other programming languages. Its primary purpose is facilitate data exchange between servers and web applications.
JSON is composed of two structures:
-
A set of name/value pairs can be represented using various data structures, including keyed lists or associative arrays.
-
An ordered collection of values can be represented using data structures such as arrays, vectors, lists, sequences.
JSON in Python
Python offers several packages that support JSON, including metamagic.json, jyson, simplejson, Yaji-Py, ultrajson, and JSON.
The following is an example of JSON data. Its structure closely resembles that of Python dictionaries.
{ "tutorialspoint": [ {"article": "1", "category": "python", "writtenby": "abc"}, {"article": "2", "category": "Java", "writtenby": "xyz"}, ], "address": [{"place": "hitechcity", "websitelink": "www.tutorialspoint.com"}], }
Python's JSON library is utilized for data serialization. JSON stands for JavaScript Object Notation.
Encoding refers to the process of converting a Python object into JSON format. Python's json module [provides functionality for working with JSON objects. However, before utilizing the module, it must first be imported.
Encoding is performed using the json.dumps() method from Python's JSON library provides predefined conversions for Python objects into JSON format.
Python | JSON |
---|---|
dict | Object |
tuple | Array |
list | Array |
Unicode | String |
number - int, long | number - int |
float | number - real |
True | True |
False | False |
None | Null |
Converting Python Tuple to JSON
To convert a Python tuple into JSON format, use the json.dumps() function. This method accepts a tuple as a parameter and converts it into a JSON string.
To work with the JSON-related functions in Python, we need to import the json module at the beginning of our code.
Following is the syntax ?
jsonStr = json.dumps(tuple)
Example
The following program uses the json.dumps() function in Python to convert an input tuple into JSON. It imports the json module, performs the conversion, and prints the resulting JSON string. -
# importing json module import json # input tuple inputTuple = ("hello", "tutorialspoint", "python") # converting input tuple into json string jsonObject = json.dumps(inputTuple) # printing the resultant JSON string print(jsonObject) print(type(jsonObject))
The result is generated as follows -
["hello", "tutorialspoint", "python"] <class 'str'>
Converting Python Tuples to JSON String
If our Python tuple contains multiple data types, we can use the json.dumps() method to convert it into a JSON string.
Example
The following program converts a tuple containing various data types - such as String, Integer, Boolean, and float - into a JSON string using Json module, performs the conversion, and prints both the result and its type.
# importing json module import json # input tuple containing several datatypes(Heterogenous data types) inputTuple = (5, 9.5, "tutorialspoint", False) # converting input tuple into json string jsonObject = json.dumps(inputTuple) # printing the resultant JSON string print(jsonObject) # printing the type of resultant JSON string print(type(jsonObject))
Output
The result is generated as follows ?
[5, 9.5, "tutorialspoint", false] <class 'str'>
Extracting Data from JSON Using json.loads()
If a Python tuple contains multiple data types, we can convert it into a JSON string using the json.dumps() method.
Example
The following program parses a JSON string and accesses its elements using the json.loads() method. It imports the json module, converts a heterogeneous tuple into a JSON string using json.dumps(), then converts it back into a list using json.loads.
# importing json module import json # input tuple containing sevaral datatypes inputTuple = (5, 9.5, "tutorialspoint", False) # converting input tuple into json string jsonObject = json.dumps(inputTuple) # printing the resultant JSON string print(jsonObject) # printing the type of resultant JSON string print(type(jsonObject)) print("Converting JSON string object into list:") # converting JSON string object into list using json.loads() function jsonArray= json.loads(jsonObject) # accessing the first element of JSON array print("First element of JSON array:", jsonArray[0]) # printing the type of json array print("Type of json array:", type(jsonArray))
The result is generated as follows -
[5, 9.5, "tutorialspoint", false] <class 'str'> Converting JSON string object into list: First element of JSON array: 5 Type of json array: <class 'list'>