2 ms·
`json.loads` in Python exists, and Python does the intuitive thing when you do `{"a": 1} == {"a": 1}`, at least for most purposes (you want the other option? `i
by rtpg 3y ago
`json.loads` in Python exists, and Python does the intuitive thing when you do `{"a": 1} == {"a": 1}`, at least for most purposes (you want the other option? `is` is right there!). Stuff like argparse is not the easiest thing to use but it's in the standard library and relatively easy to use as well.
Not going to outright say that node.js scripts are the worst thing ever (they're not), but out-of-the-box Python is totally underrated (except on MacOS where `urllib` fails with some opaque errors untill you run some random script to deal with certs)
- peferron 3y agoI haven't had a great experience dealing with JSON in Python, but maybe I'm doing it wrong. What would be the Python equivalent of this JS code? JSON.parse(<data>).foo?.[0]?.bar Basically just return the `bar` field of the the first element of `foo`, or None/undefined if it doesn't exist.
- PurpleRamen 3y agoAssuming <data> will be a key-value-object aka dict, it would be something like this: import json data = json.loads('<data>') bar = None if foo:=data.get('foo'): bar = foo[0].bar print(bar) If you can't be sure to get a dict, another type-check would be necessary. If you read from a file or file-like-object (like sys.stdin), json.load should be used.