# How should/can I convert loaded JSON data into Python objects?

**URL:** <https://discuss.python.org/t/how-should-can-i-convert-loaded-json-data-into-python-objects/20307>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 25, 2022, 11:40am UTC](https://discuss.python.org/t/how-should-can-i-convert-loaded-json-data-into-python-objects/20307 "2022-10-25T11:40:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John15321](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/john15321/32/9488_2.png) [@John15321](https://discuss.python.org/u/John15321)\
**Post date:** [October 25, 2022, 11:40am UTC](https://discuss.python.org/t/how-should-can-i-convert-loaded-json-data-into-python-objects/20307/1 "2022-10-25T11:40:45Z")

</div>

I have JSON input like this:

```json
{
  "accountID": "001-002-003-004",
  "accountClass": "Primary",
  "id": "1-2",
  "openTime": "2019-12-21T02:12:17.122Z",
  "priceDifference": "0.12345",
}

```

I would like to deserialise it into a Python object in a way similar to how `serde` from Rust works.

I would like to define a class like this:

```python
@dataclass
class MyClass:
    accountID: str
    accountClass: str
    id: str
    openTime: str
    priceDifference: float

```

After loading the JSON data what is the best way to serialise it into that classes object?  
I know I can use kwargs for that like this:

```python
MyClass(**json_data)

```

But one of the pains here is that the names would have to match therefore I cannot have snake case for names of the fields in my class (or maybe there is a way to rename those, but I dont know how).  
Is there a better way to do that? Also is there a way in which I could keep the snake case naming for my class without changing the names of the JSON fields?

---

<div class="post-metadata">

**Author:** ![Melendowski](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/melendowski/32/2960_2.png) [@Melendowski](https://discuss.python.org/u/Melendowski)\
**Post date:** [October 25, 2022, 11:44am UTC](https://discuss.python.org/t/how-should-can-i-convert-loaded-json-data-into-python-objects/20307/2 "2022-10-25T11:44:56Z")

</div>

Pydantic can help here

> **[pydantic](https://pydantic-docs.helpmanual.io/)**
>
> Data validation and settings management using Python type hints

They have a section that provides a solution for your expect example

> **[Model Config - pydantic](https://pydantic-docs.helpmanual.io/usage/model_config/#alias-generator)**
>
> Data validation and settings management using Python type hints

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [October 25, 2022, 1:11pm UTC](https://discuss.python.org/t/how-should-can-i-convert-loaded-json-data-into-python-objects/20307/3 "2022-10-25T13:11:02Z")

</div>

If you do not want to use an external package for that, here are solutions which you can extend with field name translation:

> <https://stackoverflow.com/questions/68417319/initialize-python-dataclass-from-dictionary>

---

<div class="post-metadata">

**Author:** ![strokirk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/strokirk/32/3809_2.png) [@strokirk](https://discuss.python.org/u/strokirk)\
**Post date:** [October 26, 2022, 6:57pm UTC](https://discuss.python.org/t/how-should-can-i-convert-loaded-json-data-into-python-objects/20307/4 "2022-10-26T18:57:31Z")

</div>

While pydantic and similar dedicated schema libraries are probably the cleanest solutions to the problem, I’d like to throw in a recommendation for [python-box · PyPI](https://pypi.org/project/python-box/) as well. It is wonderful to use when exploring a new data structure.

---

<div class="post-metadata">

**Author:** ![nrbnlulu](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nrbnlulu/32/12042_2.png) [@nrbnlulu](https://discuss.python.org/u/nrbnlulu)\
**Post date:** [April 27, 2023, 12:32pm UTC](https://discuss.python.org/t/how-should-can-i-convert-loaded-json-data-into-python-objects/20307/5 "2023-04-27T12:32:02Z")

</div>

there is [msgspec](https://github.com/jcrist/msgspec) / [cattrs](https://github.com/python-attrs/cattrs) / [GitHub - ltworf/typedload: Python library to load dynamically typed data into statically typed data structures](https://github.com/ltworf/typedload)

use pydantic only if you need heavy validation.
