Build nested dictionary in shell script
<p>My aim is to create a dictionary structure as mentioned below in shell script and pass that as an argument to a python script. I can build that dictionary in python script itself by passing all variable value from shell. Actually that will be too much of arguments for my python script.</p>

<pre><code>{ 
   "a": "Hello",
   "b": { 
      "c": { 
         "key": "value1"
      }, 
      "d": { 
         "key2": "value2"
      }
   }
}
</code></pre>

<p>Where <code>a, b, c, d, key2, key2</code> are string values stored in different variables.</p>

<p>Is there any way to achieve this?</p>

<p>You can create a json file with your dictionary and pass to python script with argparse</p>

<p>For example:</p>

<pre><code>import os
import argparse
import json


def execute(json_file):
    if os.path.isfile(json_file):
        with open(json_file) as json_data:
            data = json.load(json_data)
            # here you can process your dictionary data
            print data


parser = argparse.ArgumentParser(description="This script will take json file as argument")
parser.add_argument('json')
arg = parser.parse_args()
json_file = arg.json
execute(json_file)
</code></pre>

<p>open command shell and enter:</p>

<pre><code>C:\Python27\python.exe argDict.py C:\temp.json
</code></pre>

python shell dictionary