How to convert JSON into multiple dictonaries using Python
<p>I'd like to take the following JSON and convert it into multiple dicts so I can access each setting under the top level nodes only for that environment.  This is a config file that will maintain settings for different environments, I'd like to be able to grab a top level node/environment and then use all the underlying nodes/settings just for that environment.</p>

<p>My example JSON</p>

<blockquote>
  <p>{   "default": </p>
  
  <p>{</p>

<pre><code>"build": {
  "projectKey": "TEST",
  "buildKey": "ME"
},
"headers": {
  "json": "application/json",
  "xml": "application/xml"
}
</code></pre>
  
  <p>},</p>
  
  <p>"dev": {</p>

<pre><code>"build": {
  "projectKey": "TEST",
  "buildKey": "YOU"
},
"headers": {
  "json": "application/json",
  "xml": "application/xml"
}
</code></pre>
  
  <p>},</p>
  
  <p>"qa": {</p>

<pre><code>"build": {
  "projectKey": "TEST",
  "buildKey": "THEM"
},
"headers": {
  "json": "application/json",
  "xml": "application/xml"
}
</code></pre>
  
  <p>} 
  }</p>
</blockquote>

<p>I tried doing this by pulling out the top level keys but couldn't see how to break them up into multiple dictionaries using Python so I could collect each environment's settings and use them without duplication.  Checking the underlying nodes I could see doing by checking the len of the node, to see if there are any more nodes underneath, but from the top level and splitting each one to its own dict I couldn't work out.</p>

<p>Or perhaps there is a better way to do this, than I am not aware of.  The length underneath could vary, but that should be irrelevant if I can split these up.</p>

<p>Store above data within a variable like below and convert it to dict using json.loads
It converts text to dictionary, then you can loop over dictionary based on env to get the properties</p>

<pre><code>text='''
{ "default":

{

"build": {
  "projectKey": "TEST",
  "buildKey": "ME"
},
"headers": {
  "json": "application/json",
  "xml": "application/xml"
}

},

"dev": {

"build": {
  "projectKey": "TEST",
  "buildKey": "YOU"
},
"headers": {
  "json": "application/json",
  "xml": "application/xml"
}

},

"qa": {

"build": {
  "projectKey": "TEST",
  "buildKey": "THEM"
},
"headers": {
  "json": "application/json",
  "xml": "application/xml"
}

} }'''
import json
dict=json.loads(text)
</code></pre>

python json dictionary configuration