3 ms·
The syntax has a bit of a learning curve, but there's a library called glom that can be helpful in parsing heterogeneous JSON. Specifically it has a coalesce op
by cameroncairns 3y ago
The syntax has a bit of a learning curve, but there's a library called glom that can be helpful in parsing heterogeneous JSON. Specifically it has a coalesce operator that let's you define multiple paths to get the data you are trying to get at, with a default value if none of the paths are valid.
https://glom.readthedocs.io/en/latest/api.html#defaults-with-coalesce https://glom.readthedocs.io/en/latest/api.html#defaults-with...
e.g.
target = [{"a": {"b": "c"}}, {"a": {"c": "e"}}, {"a": {}}]
results = glom(target, [Coalesce("a.b", "a.c", default="")]) # -> ["c", "e", ""]