Skip to content

Convert your json/dict keys recursively or strings to camelCase or snakeCase

License

Notifications You must be signed in to change notification settings

rafa-acioly/animal_case

Repository files navigation

Animal case convert recursively your dict/json keys to camelCase or snakeCase.

CircleCI

Most commonly used to build proxies when we need to create communication between apis that have different syntaxes on their endpoint.

Imagine that you have to make a get request on some endpoint and send a post request to another endpoint with some mutate data comming from the first request but each of there endpoints have differents json key sintaxies, now you have to convert all the keys recursively and the hell begins...fear no more my friend.

Run tests

python pytest -m tests/

Usage:

Converting dict keys recursively

By default parse_keys convert keys to snake_case

from animal_case import parse_keys

my_dict = {
    "firstKey": "first value",
    "secondKey": "second value",
    "thirdKey": [
        {"subThirdKey": 1},
        {"subThirdKey2": 2},
        {"subThirdKey3": [
                {"superDeep": "wow"}
            ]
        }
    ]
}

converted = parse_keys(my_dict)
# output
'''
{
    "first_key": "first value",
    "second_key": "second value",
    "third_key": [
        {"sub_third_key": 1},
        {"sub_third_key2": 2},
        {"sub_third_key3": [
                {"super_deep": "wow"}
            ]
        }
    ]
}
'''
from animal_case import parse_keys
from animal_case.types import CAMEL_CASE

my_dict = {
    "first_key": "first value",
    "second_key": "second value",
    "third_key": [
        {"sub_third_key": 1},
        {"sub_third_key2": 2},
        {"sub_third_key3": [
                {"super_deep": "wow"}
            ]
        }
    ]
}

converted = parse_keys(my_dict, types=CAMEL_CASE)
# output
'''
{
    "firstKey": "first value",
    "secondKey": "second value",
    "thirdKey": [
        {"subThirdKey": 1},
        {"subThirdKey2": 2},
        {"subThirdKey3": [
                {"superDeep": "wow"}
            ]
        }
    ]
}
'''

snake case

from animal_case import to_snake_case

converted = to_snake_case('myRandomString')
print(converted) # output: my_random_string

camel case

from animal_case import to_camel_case

converted = to_camel_case('my_random_string')
print(converted) # output: myRandomString