Convert List Of Tuples To Json String in Python
Last Updated :
01 Mar, 2024
We have a list of tuples and our task is to convert the list of tuples into a JSON string in Python. In this article, we will see how we can convert a list of tuples to a JSON string in Python.
Convert List Of Tuples To Json String in Python
Below, are the methods of Convert List Of Tuples To Json String In Python:
Convert List Of Tuples To Json String Using map() Function
In this example, below Python code below converts a list of tuples into a JSON string using the `JSON` module. It employs a mapping function to transform each tuple into a dictionary, then uses `map` and `below n.dumps` for the conversion. The resulting JSON string is printed.
Python3
import json
list_of_tuples = [( 1 , 'apple' ), ( 2 , 'banana' ), ( 3 , 'cherry' )]
def tuple_to_dict(tpl):
return {tpl[ 0 ]: tpl[ 1 ]}
json_string = json.dumps( list ( map (tuple_to_dict, list_of_tuples)))
print ( type (list_of_tuples))
print (json_string)
print ( type (json_string))
|
Output
<class 'list'>
[{"1": "apple"}, {"2": "banana"}, {"3": "cherry"}]
<class 'str'>
Convert List Of Tuples To Json String Using dict() Constructor
In this example, below Python code uses the `json` module to convert a list of tuples into a JSON string. It first transforms the list into a dictionary and then utilizes `json.dumps()` to generate the JSON string. The final result is printed, demonstrating a concise way to handle such conversions.
Python3
import json
list_of_tuples = [( 1 , 'apple' ), ( 2 , 'banana' ), ( 3 , 'orange' )]
dictionary_data = dict (list_of_tuples)
json_string = json.dumps(dictionary_data)
print ( type (list_of_tuples))
print ( type (json_string))
print (json_string)
|
Output
<class 'list'>
<class 'str'>
{"1": "apple", "2": "banana", "3": "orange"}
Convert List Of Tuples To Json String Using Non-String Keys and Values
In this approach, a list of tuples with mixed data types is converted into a dictionary. To ensure compatibility with JSON, both keys and values are explicitly converted to strings before creating the dictionary. The resulting dictionary is then converted to a JSON string using json.dumps().
Python3
import json
list_of_tuples = [( 'a' , 1 ), ( 2 , 'b' ), ( 'c' , True )]
mixed_dictionary_data = { str (key): str (value) for key, value in list_of_tuples}
json_string = json.dumps(mixed_dictionary_data)
print ( type (list_of_tuples))
print ( type (json_string))
print (json_string)
|
Output
<class 'list'>
<class 'str'>
{"a": "1", "2": "b", "c": "True"}
Conclusion
In conlcusion, Converting a list of tuples to a JSON string in Python can be achieved through various methods, each offering flexibility based on your specific requirements. Whether you prefer the simplicity of the json
module or the more explicit approaches involving list comprehensions or the map
function, understanding these methods provides you with the tools to efficiently work with JSON data in your Python applications.
Share your thoughts in the comments
Please Login to comment...