Data Format Converter · Tutorial

How to read a pyproject.toml file as JSON

5 min readbeginnerUpdated

A pyproject.toml holds a Python project's name, version, dependencies and settings for tools like Ruff and pytest. This tutorial converts one to JSON, explains how tables such as [tool.pytest.ini_options] turn into nested objects, and shows how to use the result in a script or CI step.

What you need

  • The Data Converter, set to TOML → JSON
  • A pyproject.toml file, or the sample from the Try it button

Steps

  1. Paste the file

    Paste the contents of pyproject.toml into the left box, or open the file with the file picker. The JSON appears on the right straight away.

  2. See how the tables nest

    Each table name in square brackets is a path. [project.optional-dependencies] puts a optional-dependencies object inside project, and [tool.pytest.ini_options] creates tool, then pytest, then ini_options.

    [tool.pytest.ini_options]
    addopts = "-q"
    
    "tool": { "pytest": { "ini_options": { "addopts": "-q" } } }
  3. Find the values you need

    Dependencies are now a plain array at project.dependencies, and the Ruff line length is the number 100 at tool.ruff.line-length. Keys keep their original spelling, dashes included.

  4. Use the JSON elsewhere

    Press Copy or Download. In a script you can read a value with a JSON tool, for example jq -r '.project.version' converted.json prints 1.4.0. This is handy in CI steps that need the version number for a release tag.

  5. Fix errors if the file is broken

    If the message shows an error, such as a duplicate key, it names the line and column. Fix it in the left box. The same mistake would also stop pip install or your build tool.

Try it yourself

Opens the Data Format Converter with this tutorial's sample loaded.

Try it now

Common mistakes

Expecting keys with dashes to be renamed
requires-python stays requires-python. In JavaScript you need data.project["requires-python"].
Declaring the same table twice
TOML does not allow two [project] headers. Merge their keys under one header.
Unquoted strings
Unlike INI or YAML, TOML needs quotes around text. name = bento-tools is an error; write name = "bento-tools".

Next steps

Go back to the Data Format Converter, or keep going with one of these:

FAQ

Does it work for Cargo.toml too?

Yes. Any valid TOML file converts, including Cargo.toml, Hugo and Netlify configs.

Can I convert the JSON back to TOML?

Yes, with JSON → TOML in the Data Converter. Comments are not restored, so keep the original file.