Is It Possible To Use A Variable Such As "['time']['updated']" To Parse Json?
Ok, json_tree is a variable that contains something that looks like this: json_tree = '['time']['updated']' Can you pass it as a variable.... hdr = { 'User-Agent' : 'Mozil
Solution 1:
This is a use case for jsonpath
, in which syntax one would use the expression time.updated
to refer to the value in question.
json_expr = "time.updated"
json_dict = {"time": {"updated": "2018-01-05"}}
from jsonpath_rw import parse as parse_jsonpath
results = parse_jsonpath(json_expr).find(json_dict)
if len(results) == 0:
raise Exception("Could not find any matches for %r in %r" % (json_expr, json_dict))
elif len(results) > 1:
raise Exception("Expression %r had more than one match; cannot use for configuration" % (json_expr,))
result = results[0].value
Post a Comment for "Is It Possible To Use A Variable Such As "['time']['updated']" To Parse Json?"