Last active
December 23, 2025 18:19
-
-
Save eqvinox/8f307917d0bdfb78bba6e759b5f00648 to your computer and use it in GitHub Desktop.
Flatten [{"data": foo}] to foo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| def flatten_data(pairs): | |
| for i, (k, v) in enumerate(pairs): | |
| if not isinstance(v, list) or len(v) != 1 or not isinstance(v[0], dict): | |
| continue | |
| kk, vv = next(iter(v[0].items())) | |
| if kk == "data": | |
| pairs[i] = (k, vv) | |
| return dict(pairs) | |
| with open("data.json", "r") as fd: | |
| d = json.load(fd, object_pairs_hook=flatten_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment