Registry / serialization / camel-converter

camel-converter

JSON →
library5.1.1pypypi✓ verified 25d ago

Camel Converter is a Python library designed to effortlessly convert strings and dictionary keys between camel case, snake case, and pascal case. It's particularly useful for handling JSON data where keys are often in camelCase, facilitating seamless integration with Python's snake_case conventions. The library also provides decorators for automatic dictionary key conversion in function returns and offers integration with Pydantic models. The current version is 5.1.0.

pip install camel-converter
INSTALL
IMPORT
SIG · CAMEL-CONVERTER
C
camel-converter
serializationpythonv5.1.1
Install
1.5s avg
Import
12ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v5.1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.008s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

to_snake
from camel_converter import to_snake
to_camel
from camel_converter import to_camel
to_pascal
from camel_converter import to_pascal
dict_to_snake
from camel_converter import dict_to_snake
dict_to_camel
from camel_converter import dict_to_camel
dict_to_pascal
from camel_converter import dict_to_pascal
dict_to_snake (decorator)
from camel_converter.decorators import dict_to_snake
dict_to_camel (decorator)
from camel_converter.decorators import dict_to_camel
dict_to_pascal (decorator)
from camel_converter.decorators import dict_to_pascal
CamelBase
from camel_converter.pydantic_base import CamelBase
Requires Pydantic to be installed separately.

This quickstart demonstrates basic string and dictionary key conversion using `to_snake`, `to_camel`, `dict_to_snake`, and decorator-based conversion with `dict_to_camel`. It also includes an example of using `CamelBase` for Pydantic models, which automatically handles camelCase to snake_case mapping for model instantiation.

from camel_converter import to_snake, to_camel, dict_to_snake from camel_converter.decorators import dict_to_camel from pydantic import BaseModel # For CamelBase example # String conversion snake_case_string = to_snake('myCamelCaseString') print(f"'myCamelCaseString' to snake_case: {snake_case_string}") camel_case_string = to_camel('my_snake_case_string') print(f"'my_snake_case_string' to camelCase: {camel_case_string}") # Dictionary key conversion camel_dict = {'firstName': 'John', 'lastName': 'Doe', 'dateOfBirth': '1990-01-01'} snake_dict = dict_to_snake(camel_dict) print(f"CamelCase dict to snake_case: {snake_dict}") # Decorator usage @dict_to_camel def get_data_from_db() -> dict: # Simulate fetching data with snake_case keys return {'user_id': 123, 'user_name': 'test_user'} api_response = get_data_from_db() print(f"Decorated function output (snake_case to camelCase): {api_response}") # Pydantic integration (requires 'pydantic' to be installed) try: from camel_converter.pydantic_base import CamelBase class User(CamelBase): first_name: str last_name: str user_data_camel = {'firstName': 'Jane', 'lastName': 'Smith'} user_obj = User(**user_data_camel) print(f"Pydantic CamelBase model from camelCase input: {user_obj.first_name} {user_obj.last_name}") except ImportError: print("Pydantic not installed. Skipping CamelBase example.")
Debug
Known issues
gotchaThe library directly converts strings or dictionary keys. For converting complex nested structures (lists of dictionaries, deeply nested dictionaries), ensure you apply the conversion functions recursively or use tools built for such scenarios.
fix
For deeply nested structures, manually apply `dict_to_snake` or `dict_to_camel` at each level, or consider a custom recursive function.
affects: All
gotchaWhen using `dict_to_snake`, `dict_to_camel`, or `dict_to_pascal`, only string keys are converted. Non-string keys (e.g., integers, tuples) in a dictionary will be left unchanged in the output.
fix
Be aware that dictionaries with mixed key types will only have their string keys transformed. If non-string keys need transformation (e.g., `(1, 'someValue')` to `(1, 'some_value')`), convert them explicitly before or after processing.
affects: All
gotchaThe `CamelBase` Pydantic integration requires Pydantic to be installed as an additional dependency. It is not part of the `camel-converter` core package dependencies.
fix
Install Pydantic separately via `pip install pydantic` if you intend to use `CamelBase` for Pydantic model definitions.
affects: All
gotchaThe library explicitly requires Python 3.10 or newer. Attempting to install or run it on older Python versions will result in compatibility errors.
fix
Ensure your environment uses Python 3.10 or a newer version.
affects: <5.0.0 (older versions might support earlier Python), >=5.0.0 (requires >=3.10)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'camel_converter'
The 'camel-converter' library is not installed in your current Python environment.
fix
Run `pip install camel-converter` to install the package.
ModuleNotFoundError: No module named 'camel_converter.pydantic_base'
The optional 'pydantic' extra for camel-converter was not installed, which is required to use the CamelBase class for Pydantic integration.
fix
Install camel-converter with the pydantic extra: `pip install camel-converter[pydantic]`.
Keys in nested dictionaries are not converted (e.g., {'outerKey': {'innerKey': 'value'}} becomes {'outer_key': {'innerKey': 'value'}})
The `dict_to_snake`, `dict_to_camel`, and `dict_to_pascal` functions in `camel-converter` only perform a shallow conversion of top-level dictionary keys by default and do not recursively process nested dictionaries.
fix
Manually apply the conversion functions recursively to each nested dictionary, or implement a custom recursive utility function for deep conversion. The library does not provide a built-in deep conversion for dictionaries.
Non-string dictionary keys are not converted (e.g., {1: 'value', 'stringKey': 'value'} becomes {1: 'value', 'string_key': 'value'})
The `dict_to_snake`, `dict_to_camel`, and `dict_to_pascal` functions in `camel-converter` are designed to only convert string keys within dictionaries, leaving non-string keys (like integers or tuples) unchanged.
fix
Ensure that all dictionary keys you intend to convert are strings. If non-string keys require transformation, convert them to strings before processing with `camel-converter` or handle them explicitly.
Upgrade
Version history
5.1.1latest on PyPI · released Aug 25, 2026
Audit
Dependencies
pydanticoptionalRequired for using the `CamelBase` Pydantic model integration.
Agent activity
18 hits · last 30 days
node
14
OpenAI (training)
1
Resources
camel-converter — pip install camel-converter · libregistry