Content-Length: 298212 | pFad | https://github.com/python/cpython/issues/96145

F2 Add AttrDict to JSON module for use with object_hook · Issue #96145 · python/cpython · GitHub
Skip to content

Add AttrDict to JSON module for use with object_hook #96145

@rhettinger

Description

@rhettinger

AttrDict is used improve the readability of nested data access from JSON files. Compare:

 config.servers.setup.interfaces.mgmt.protocol
 config["servers"]["setup"]["interfaces"]["mgmt"]["protocol"]

Victor's research showed that tooling this is very popular in Python world and the implementation dates back quite far. At first I suggested I suggested extending SimpleNamespace(); instead, Guido recommended putting this is the JSON module which is where it would primarily be used:

with open('kepler.json') as f:
    kepler = json.load(f, object_hook=AttrDict)
print(kepler.orbital_period.neptune)

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Done

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/python/cpython/issues/96145

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy