Skip to content

Instantly share code, notes, and snippets.

@eqvinox
Last active December 23, 2025 18:19
Show Gist options
  • Select an option

  • Save eqvinox/8f307917d0bdfb78bba6e759b5f00648 to your computer and use it in GitHub Desktop.

Select an option

Save eqvinox/8f307917d0bdfb78bba6e759b5f00648 to your computer and use it in GitHub Desktop.
Flatten [{"data": foo}] to foo
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