Open In App

Convert List Of Tuples To Json String in Python

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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
 
# Sample list of tuples
list_of_tuples = [(1, 'apple'), (2, 'banana'), (3, 'cherry')]
 
# Define a mapping function
def tuple_to_dict(tpl):
    return {tpl[0]: tpl[1]}
 
# Convert to JSON string using map and dumps
json_string = json.dumps(list(map(tuple_to_dict, list_of_tuples)))
 
print(type(list_of_tuples))
 
# Display the result
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
 
# Sample list of tuples
list_of_tuples = [(1, 'apple'), (2, 'banana'), (3, 'orange')]
 
# Convert the list of tuples to a dictionary
dictionary_data = dict(list_of_tuples)
 
# Convert the dictionary to a JSON string
json_string = json.dumps(dictionary_data)
 
print(type(list_of_tuples))
print(type(json_string))
 
# Display the result
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
 
# Another sample list of tuples with mixed data types
list_of_tuples = [('a', 1), (2, 'b'), ('c', True)]
 
# Convert the list of tuples to a dictionary, ensuring both keys and values are strings
mixed_dictionary_data = {str(key): str(value) for key, value in list_of_tuples}
 
# Convert the dictionary to a JSON string
json_string = json.dumps(mixed_dictionary_data)
 
print(type(list_of_tuples))
print(type(json_string))
 
# Display the result
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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads