python - Shortest way to replace values in nested JSON with the particular key elegantly -
imagine nested json structure below:
node = { "0" : { "param" : { "filename" : "abc", "delimiter" : "n" }, "function" : "do something" }, "1" : { "param" : { "filename" : "def", "delimiter" : "n" }, "function" : "do something" }, "2" : { "param" : { "filename" : "ghi", "delimiter" : "n" }, "function" : "do something" } }
i wish replace filename inside node variable computed filename. example want replace values of filename in "0", "1", , "2" "example.pdf".
what elegant way or shortest way in python?
how regex?
in [29]: import re in [30]: json.loads(re.sub('(?<="filename": ")(.*?)(?=")', 'example.pdf', json.dumps(node))) out[30]: {'0': {'function': 'do something', 'param': {'delimiter': 'n', 'filename': 'example.pdf'}}, '1': {'function': 'do something', 'param': {'delimiter': 'n', 'filename': 'example.pdf'}}, '2': {'function': 'do something', 'param': {'delimiter': 'n', 'filename': 'example.pdf'}}}
this not work, if not want replace every instance.
Comments
Post a Comment