Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert a Tuple to JSON in Python

  • 15 Oct, 2025
  • Com 0
Converting Tuple to JSON in Python

To convert a tuple to JSON (JavaScript Object Notation) in Python, use the json.dumps() method. The json.dumps() method serializes objects (like tuples, lists, dict, etc.) into a JSON-formatted string and returns it.

Convert a Tuple to JSON in Python using json.dumps() method

 

import json

my_tuple = ("Apple", "Meta", "Tesla")

print(my_tuple)
# Output: ('Apple', 'Meta', 'Tesla')

print(type(my_tuple))
# Output: <class 'tuple'>

# Conversion to JSON
json_data = json.dumps(my_tuple)

print(json_data)
# Output: '["Apple", "Meta", "Tesla"]'

print(type(json_data))
# Output: <class 'str'>

In the above code, we first define a tuple with three elements and then print its type.

In the next step, we used the json.dumps() method and pass the “my_tuple” as an argument that returns a JSON-formatted string. Note that even if the output is JSON-formatted, its type in Python is string.

Nested Tuple

If the tuple is nested, it is recursively converted into nested lists.

import json

nested_tuple = ("x", (11, 21, 31), ("b", "c"))

print(nested_tuple)
# Output: ('x', (11, 21, 31), ('b', 'c'))

print(type(nested_tuple))
# Output: <class 'tuple'>

# Conversion to JSON
json_data = json.dumps(nested_tuple)

print(json_data)
# Output: '["x", [11, 21, 31], ["b", "c"]]'

print(type(json_data))
# Output: <class 'str'>

In this code, the nested tuple in the input is (11, 21, 31), and the output string contains [11, 21, 31] as a list.

Using different datatypes

Here’s a table that outlines the default conversions performed by the JSON module:

Python JSON
dict Object
tuple Array
list Array
str String
number – int, long number – int
float number – real
True true
False false
None null

Converting a tuple with mixed types to a JSON in Python

import json

my_tuple = ("Apple", 4, 2.34, True)

print(my_tuple)
# Output: ('Apple', 4, 2.34, True)

print(type(my_tuple))
# Output: <class 'tuple'>

json_data = json.dumps(my_tuple)

print(json_data)
# Output: '["Apple", 4, 2.34, true]'

print(type(json_data))
# Output: <class 'str'>

That’s all!

Post Views: 29
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert String to Bytes in Python
How to Convert Set to Dictionary in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend