json.dumps() in Python (original) (raw)

Last Updated : 13 Jan, 2026

The json.dumps() function in Python converts a Python object (such as a dictionary or list) into a JSON-formatted string. It is mainly used when you need to send data over APIs, store structured data or serialize Python objects into JSON text.

**Example: This example shows how to convert a Python dictionary into a JSON string.

Python `

import json d = {"a": "Hello", "b": "World"} s = json.dumps(d) print(s) print(type(s))

`

Output

{"a": "Hello", "b": "World"} <class 'str'>

**Explanation:

Syntax

json.dumps(obj, skipkeys=False, ensure_ascii=True, allow_nan=True, indent=None, separators=None, sort_keys=False)

**Parameters:

**Return Type: string object (str).

Examples

**Example 1: This example demonstrates how skipkeys=True ignores dictionary keys that are not JSON-compatible.

Python `

import json d = {("x", "y"): 10, "a": 1} s = json.dumps(d, skipkeys=True) print(s)

`

**Explanation:

**Example 2: This example formats JSON output to make it more readable using indentation.

Python `

import json d = {"a": 1, "b": 2, "c": 3} s = json.dumps(d, indent=4) print(s)

`

Output

{ "a": 1, "b": 2, "c": 3 }

**Explanation:

**Example 3: This example sorts dictionary keys alphabetically before converting them to JSON.

Python `

import json d = {"c": 3, "a": 1, "b": 2} s = json.dumps(d, sort_keys=True) print(s)

`

Output

{"a": 1, "b": 2, "c": 3}

**Explanation:

**Example 4: This example shows how json.dumps() converts a Python list into a JSON-formatted string, which is commonly used when sending list data through APIs.

Python `

import json lst = [10, 20, 30, 40] s = json.dumps(lst) print(s)

`