Python Json Config 'Extended Interpolation'
Question: I am currently using the Python library configparser:

    
    
    from configparser import ConfigParser, ExtendedInterpolation
    

I find the ExtendedInterpolation very useful because it avoids the risk of
having to reenter constants in multiple places.

I now have a requirement to use a Json document as the basis of the
configuration as it provides more structure.

    
    
    import json
    from collections import OrderedDict
    
    def get_json_config(file):
        """Load Json into OrderedDict from file"""
    
        with open(file) as json_data:
            d = json.load(json_data, object_pairs_hook=OrderedDict)
    
        return d
    

Does anyone have any suggestions as to the best way to implement configparser
style ExtendedInterpolation?

For example if a node in the Json contains the value ${home_dir}/lumberjack
this would copy root node home_dir and take value 'lumberjack'?


Answer: Try to use `string.Template`. But I'm not sure whether it's your need. There
is one package can do this may be. Bellow is what i should do.

`config.json`

    
    
    {
        "home_dir": "/home/joey",
        "class_path": "/user/local/bin",
        "dir_one": "${home_dir}/dir_one", 
        "dir_two": "${home_dir}/dir_two", 
    
        "sep_path_list": [
            "${class_path}/python",
            "${class_path}/ruby",
            "${class_path}/php"
        ]
    }
    

python code:

    
    
    import json
    from string import Template
    
    with open("config.json", "r") as config_file:
        config_content = config_file.read()
        config_template = Template(config_content)
        mid_json = json.loads(config_content)
        config = config_template.safe_substitute(mid_json)
    
        print config
    

This can substitute the defined key in json file.

