Xerobit

Python Dict to JSON Converter

Paste a Python dictionary or list literal to convert it to valid JSON instantly. The tool handles single quotes, True, False, None, tuples and trailing commas without any server round trip. It runs entirely in your browser so your data stays private.

Paste a Python literal using single quotes, True/False/None, tuples or trailing commas.

JSON

{
  "name": "Ada",
  "active": true,
  "scores": [
    95,
    87
  ],
  "note": null
}

By · Published · How we build and test tools

How to use the python dict to json converter

  1. Paste your Python dictionary or list literal into the text area.
  2. Adjust the indent size if you want compact or pretty-printed output.
  3. The converted JSON appears immediately below as you type.
  4. Copy the result for use in APIs, config files or other tools.

How the conversion works

A Python dict to json conversion is not a simple find-and-replace. The tool uses a small recursive-descent parser that reads the input character by character. It recognizes Python-specific tokens like True, False and None, mapping them to their JSON equivalents true, false and null. Single-quoted strings are parsed with full escape handling, including \', \\ and unicode escapes.

Tuples such as (1, 2) are emitted as JSON arrays [1, 2] because JSON has no tuple type. Trailing commas before a closing bracket or brace are silently consumed since JSON does not allow them. For example, the Python literal {'a': 1, 'b': 2,} becomes {"a": 1, "b": 2}.

Supported Python constructs

The converter handles the subset of Python literals that map cleanly to JSON. Here is a reference table of what is supported:

Python syntaxJSON outputNotes
{'k': 'v'}{"k": "v"}Single or double quoted keys and values
[1, 2][1, 2]Lists become arrays
(1, 2)[1, 2]Tuples become arrays
True / Falsetrue / falseCase-sensitive
NonenullCase-sensitive
42, 3.14, -1e342, 3.14, -1000Integers and floats
Trailing commaRemoved{'a':1,} is valid input

What causes an error

Because this is a pure text transformation and not a full Python interpreter, several constructs will produce a clear error message rather than invalid output. Function calls like dict() or len(x) are rejected. Bare variable names that are not True, False or None cause a parse failure. Set literals using curly braces without colons are not supported because JSON has no set type.

If you need to convert other configuration formats to JSON, dedicated tools exist for those tasks. For instance, use TOML to JSON for TOML files, INI to JSON for INI files, or ENV to JSON for environment variables. Those converters understand the specific grammar of each format.

Common mistakes when converting manually

Developers often try to convert a python dictionary to json using basic string replacement, which leads to subtle bugs. Replacing single quotes with double quotes breaks strings that contain apostrophes: 'it\'s' would become malformed. Another common mistake is forgetting that Python allows trailing commas while the JSON specification forbids them, causing parsers to reject the output.

Using Python's built-in json.dumps() is the standard approach on the server side, but when you only have a snippet copied from logs or a REPL session, pasting it into this converter is faster. If you need to go the other direction, the JSON to Python dict tool converts JSON back into Python syntax.

Practical use cases

Converting a python dict to json is useful in several everyday scenarios. When debugging a web application, you might copy a dictionary printed in the console and need to paste it into an API testing tool that expects JSON. Configuration files written in Python syntax sometimes need to be migrated to JSON for frontend consumption.

Data scientists working with Python notebooks frequently extract nested structures that must be shared with JavaScript-based visualization libraries. Rather than writing a throwaway script, this tool provides instant conversion. For tabular data exported from Python, you might instead need CSV to JSON or query string to JSON depending on the source format.

Worked examples

ExampleInputResult
Simple dictPython dict or list: {'name': 'Ada', 'age': 36}; Indent spaces: 2{ / "name": "Ada", / "age": 36 / }
Booleans, None and tuplePython dict or list: {'active': True, 'deleted': False, 'meta': None, 'tags': ('a', 'b')}; Indent spaces: 2{ / "active": true, / "deleted": false, / "meta": null, / "tags": [ / "a", / "b" / ] / }
Trailing commasPython dict or list: [1, 2, 3,]; Indent spaces: 0[1,2,3]
Nested structurePython dict or list: {'users': [{'id': 1, 'name': 'Bob'}, {'id': 2, 'name': 'Eve'}]}; Indent spaces: 4{ / "users": [ / { / "id": 1, / "name": "Bob" / }, / { / "id": 2, / "name": "Eve" / } / ] / }

FAQ

Does this tool handle nested dictionaries?

Yes. The parser is recursive, so arbitrarily nested dicts, lists and tuples are converted correctly to nested JSON objects and arrays.

Why does my input with a function call fail?

The converter only processes Python literals. Expressions like datetime.now() or dict(a=1) require code execution, which a static text tool cannot safely perform.

Are Python tuples converted to JSON arrays?

Yes. JSON has no tuple type, so both Python lists and tuples are emitted as JSON arrays using square brackets.

Is my data sent to a server?

No. The entire python dict to json conversion happens locally in your browser using JavaScript. Nothing is uploaded anywhere.

Can I convert a Python set to JSON?

No. JSON does not support sets. You should convert the set to a list in Python first, then paste the list literal into this tool.

What about multiline strings or triple quotes?

Triple-quoted strings are supported. They are parsed and emitted as standard JSON strings with appropriate newline escaping.