I have a nested json file and a dictionary(params) for lookup, I would like to replace record dict value where format "<>" is present with lookup.
lookup dictionary:
params_lookup = {"population":47, "year":2022, "valid":True, "end":"colls", "num":23, "items":"more"}
record:
record = {"country":"zzzx", "metrics":{"population":"<population> million", "currency":"Euro", "year":"<year>"}, "valid":"<valid>",
"others":["<num>", 2, 3], "name":"<items>"}
here is my attempt, but it fails when lookup value is integer or boolean.
I understand since replace expects string in the lookup, it fails with TypeError: replace() argument 2 must be str, not int. could you please advise on how to approach the problem.
import re
START_DELIMITER = '<'
END_DELIMITER = '>'
def format_params(d, params_lookup):
if isinstance(d, list):
return [format_params(item, params_lookup) for item in d]
if isinstance(d, dict):
return {key: format_params(value, params_lookup)
for key, value in d.items()}
if isinstance(d, str) and bool(re.search(START_DELIMITER + "\w*" + END_DELIMITER, d)):
params = re.findall(START_DELIMITER + "(\w*)" + END_DELIMITER, d)
for param in params:
if not params_lookup.get(param):
raise Exception(
f" Parameter {START_DELIMITER + param + END_DELIMITER} is not found in the mapping")
else:
d = d.replace(START_DELIMITER + param + END_DELIMITER, params_lookup.get(param))
return d
else:
return d
format_params(record, params_lookup)
desired result:
record = {"country":"zzzx", "metrics":{"population":"47 million", "currency":"Euro", "year": 2022}, "valid":True,
"others":[23, 2, 3], "name":"more"}