How to convert a Tuple to JSON in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Convert a Tuple to JSON in Python

Use the json.dumps() method to convert a tuple to JSON.

The json.dumps() method will convert the Python tuple to a JSON array and will return the result.

main.py
import json my_tuple = ('bobby', 'hadz', 'com') json_str = json.dumps(my_tuple) print(json_str) # ๐Ÿ‘‰๏ธ '["bobby", "hadz", "com"]' print(type(json_str)) # ๐Ÿ‘‰๏ธ <class 'str'>

convert tuple to json

The code for this article is available on GitHub

We used the json.dumps() method to convert a tuple to a JSON string.

The json.dumps() method converts a Python object to a JSON formatted string.

# Python tuples are JSON serializable

Python tuples are JSON serializable, just like lists or dictionaries.

The JSONEncoder class supports the following objects and types by default.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int and float derived Enumsnumber
Truetrue
Falsefalse
Nonenull

The process of converting a tuple (or any other native Python object) to a JSON string is called serialization.

Whereas, the process of converting a JSON string to a native Python object is called deserialization.

It should be noted that Python tuples get converted to a JSON array, just like lists.

# Parsing the JSON string returns a Python list

When you parse the JSON string into a native Python object, you get a list back.

main.py
import json my_tuple = ('bobby', 'hadz', 'com') # โœ… convert tuple to JSON json_str = json.dumps(my_tuple) print(json_str) # ๐Ÿ‘‰๏ธ '["bobby", "hadz", "com"]' print(type(json_str)) # ๐Ÿ‘‰๏ธ <class 'str'> # -------------------------------- # โœ… parse JSON string to native Python object parsed = json.loads(json_str) print(parsed) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] print(type(parsed)) # ๐Ÿ‘‰๏ธ <class 'list'> print(parsed[0]) # ๐Ÿ‘‰๏ธ bobby print(parsed[1]) # ๐Ÿ‘‰๏ธ hadz

parsing the json string returns python list

The code for this article is available on GitHub

The json.loads() method parses a JSON string into a native Python object.

Notice that we got a list object after parsing the JSON string.

This is because both list and tuple objects get converted to a JSON array when serialized.

You can use the tuple() class to convert the list to a tuple after parsing the JSON string.

main.py
import json my_tuple = ('bobby', 'hadz', 'com') json_str = json.dumps(my_tuple) print(json_str) # ๐Ÿ‘‰๏ธ '["bobby", "hadz", "com"]' print(type(json_str)) # ๐Ÿ‘‰๏ธ <class 'str'> # -------------------------------- # ๐Ÿ‘‡๏ธ convert to tuple parsed = tuple(json.loads(json_str)) print(parsed) # ๐Ÿ‘‰๏ธ ('bobby', 'hadz', 'com') print(type(parsed)) # ๐Ÿ‘‰๏ธ <class 'tuple'> print(parsed[0]) # ๐Ÿ‘‰๏ธ bobby print(parsed[1]) # ๐Ÿ‘‰๏ธ hadz

The example uses the tuple() class to convert the list we got after parsing the JSON string.

You can use bracket notation to access the tuple at a specific index after parsing the JSON.

Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

# Converting a mixed tuple to JSON

You can also use the json.dumps method to convert a tuple containing elements of multiple types to JSON.

main.py
import json my_tuple = ('bobby', 1, 'hadz', 2, 'com') json_str = json.dumps(my_tuple) print(json_str) # ๐Ÿ‘‰๏ธ '["bobby", 1, "hadz", 2, "com"]' print(type(json_str)) # ๐Ÿ‘‰๏ธ <class 'str'>
The code for this article is available on GitHub

However, you have to make sure that your tuple contains one of the supported types.

The JSONEncoder class supports the following objects and types by default.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int and float derived Enumsnumber
Truetrue
Falsefalse
Nonenull

If your tuple stores values of a type that is not contained in the column on the left, the default JSONEncoder won't be able to convert the tuple to JSON.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev