Post

Single BaseModel for nested JSON

Suppose a dataset looks something like this, with no overlapping keys:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"rows": [
    {
        "a": "1234ABC",
        "b": 5.768,
        "c": 1,
        "d": [
            {
                "e": "GHT5678",
                "f": "F0000123",
                " g": 1
            },
            {
                "e": "GHT5679",
                "f": "F0000124",
                "g": 2
            },
            {
                "e": "GHT5680",
                "f": "F0000125",
                "g": 3
            },
        ]
    }
]

A single BaseModel can be created

1
2
3
4
5
6
7
8
9
Example(BaseModel):
    a: str
    b: float
    c: int
    e: str
    f: str
    g: int

Instead of iterating on each key or defining the parent and child into separate data classes, both can be read as dictionaries, merged, and then unpacked into Example:

1
2
3
4
5
6
7
8
data = list()
for row in rows:
	for child in row.get("d"):
		_d = {
			**{k: row[k] for k in row if k != "d"},
			**d
		}
		data.append(Example(**_d))
This post is licensed under CC BY 4.0 by the author.