Python: Flatten and Parse certain sections of JSON
<p>I have an input JSON that looks like this:</p>

<pre><code>&gt; {"payment": {"payment_id": "AA340", "payment_amt": "20", "chk_nr": "321749", "clm_list": {"dtl": [{"clm_id": "1A2345", "name": "John", adj:{"adj_id":"W123","adj_cd":"45"}}, {"clm_id": "9999", "name": "Dilton", adj:{"adj_id":"X123","adj_cd":"5"}}]}}}
</code></pre>

<p>I need the output to look like this:</p>

<pre><code>{"clm_id": "1A2345",adj:{"adj_id":"W123"},"payment_amt": "20", "chk_nr": "321749"}
{"clm_id": "9999"adj:{"adj_id":"X123"},"payment_amt": "20", "chk_nr": "321749"}
</code></pre>

<p>So the code takes in the one JSON doc, parses the claim array section and normalizes it by adding payment info to each section. Even the nested JSON is parsed.</p>

<p>I'm able to parse the data, but unsure on how to normalize only certain section of the data.</p>

<p>The code below will parse the data, but NOT normalize</p>

<pre><code>keep = ["payment","payment_id","payment_amt", "clm_list", "dtl", "clm_id","adj","adj_id"]

old_dict={"payment": {"payment_id": "AA340", "payment_amt": "20", "chk_nr": "321749", "clm_list": {"dtl": [{"clm_id": "1A2345", "name": "John", "adj": {"adj_id": "W123", "adj_cd": "45"}}, {"clm_id": "9999", "name": "Dilton", "adj": {"adj_id": "X123", "adj_cd": "5"}}]}}}

def recursively_prune_dict_keys(obj, keep):
        if isinstance(obj, dict):
                return dict([(k, recursively_prune_dict_keys(v, keep)) for k, v in obj.items() if k in keep])
        elif isinstance(obj, list):
                return [recursively_prune_dict_keys(item, keep) for item in obj]
        else:
                return obj

new_dict = recursively_prune_dict_keys(old_dict, keep)
conv_json=new_dict["payment"]
print json.dumps(conv_json)
</code></pre>

<p>It may be the neat way is to simply pick through the data, like;</p>

<pre><code>new_dict = recursively_prune_dict_keys(old_dict, keep)
payment = old_dict['payment']
claims = payment['clm_list']['dtl']
for claim in claims:
    claim['payment_amt'] = payment['payment_amt']
    claim['chk_nr'] = payment['chk_nr']
print(json.dumps(claims))
</code></pre>

<p>This will yield;</p>

<pre><code>[{"chk_nr": "321749", "clm_id": "1A2345", "payment_amt": "20", "adj": {"adj_id": "W123"}}, {"chk_nr": "321749", "clm_id": "9999", "payment_amt": "20", "adj": {"adj_id": "X123"}}]
</code></pre>

<p>This contains the output you asked for, but not exactly as you may want to see it.  </p>

<p>First, your desired output isn't correct JSON without the square brackets <code>[]</code> that would make it a list.  But, we can get rid of that by dumping each claim individually;</p>

<pre><code>new_dict = recursively_prune_dict_keys(old_dict, keep)
payment = old_dict['payment']
claims = payment['clm_list']['dtl']
for claim in claims:
    claim['payment_amt'] = payment['payment_amt']
    claim['chk_nr'] = payment['chk_nr']
    print(json.dumps(claim))
</code></pre>

<p>This gives;</p>

<pre><code>{"name": "John", "clm_id": "1A2345", "payment_amt": "20", "adj": {"adj_cd": "45", "adj_id": "W123"}, "chk_nr": "321749"}
{"name": "Dilton", "clm_id": "9999", "payment_amt": "20", "adj": {"adj_cd": "5", "adj_id": "X123"}, "chk_nr": "321749"}
</code></pre>

<p>This is close to your desired output, except maybe for the ordering.  Python dicts are not inherently ordered. You can sort them, however.  So, if the ordering is important, you will want to read through <a href="http://pythoncentral.io/how-to-sort-python-dictionaries-by-key-or-value/" rel="nofollow">How to Sort Python Dictionaries by Key or Value</a></p>

python json parsing flatten