3 ms·
Excepting the cringemarsh that is "import <jsonfile>", this is actually very useful. This looks very unpythonic: my_json["this"]["can"]["be"] == "nested" and
by StavrosK 11y ago
Excepting the cringemarsh that is "import <jsonfile>", this is actually very useful. This looks very unpythonic:
my_json["this"]["can"]["be"] == "nested"
and is impossible to write defensively and cleanly after the first call:
my_json.get("this", {}).get("no way to not crash here")
Something that could do:
my_json.this.can.be == "nested"
With a default value would be very useful. Does anyone know something like it, or should I whip a module up in an hour?
- dadrian 11y agoI've taken a crack at something similar, I'd love to see what you came up with. I ended up with this super-meh solution: https://github.com/zmap/ztag/blob/master/ztag/transform.py#L7 https://github.com/zmap/ztag/blob/master/ztag/transform.py#L...
- StavrosK 11y agoWhat I had in mind sounds pretty close to yours, really. I was thinking of adding a default somewhere along the line (possibly the end), so you could do: json.structure.that.i.want.to.access.default(True) Or something like that. I don't really like the default item API I've come up... I'll comment here if I do give it a go, thanks for the snippet!
- StavrosK 11y agoMy thing turned out to be pretty much the same as yours, also equally meh: https://github.com/skorokithakis/jsane https://github.com/skorokithakis/jsane There's no code there, but the underlying class is pretty much spot on yours. I'm still trying to improve the API, but it's not looking great.