2016-10-11 77 views
0

的某些部分我有一個輸入JSON看起來像這樣:的Python:扁平化和解析JSON

> {"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"}}]}}} 

我需要的輸出看起來像這樣:

{"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"} 

因此,代碼發生在一個JSON文檔解析聲明數組部分,並通過向每個部分添加付款信息來對其進行標準化。甚至嵌套的JSON也被解析。

我能夠解析數據,但不確定如何正常化只有某些部分的數據。

下面的代碼將解析的數據,但不規範化

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) 
+0

如果您需要幫助,您需要發佈[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)你試過的代碼 – CAB

+0

更新了代碼 – user3646519

回答

0

它可能是整潔的方式是簡單地通過數據接,等;

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)) 

這會產生;

[{"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"}}] 

這包含您要求的輸出,但不完全如您希望看到的那樣。

首先,您所需的輸出是不正確的JSON沒有方括號[],這將使它成爲一個列表。但是,我們可以通過逐個傾銷每項索賠來擺脫這種情況;

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)) 

這給出了;

{"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"} 

這是接近你想要的輸出,除了可能的排序。 Python字典不是固有的有序。不過,您可以對它們進行排序。因此,如果排序很重要,您需要閱讀How to Sort Python Dictionaries by Key or Value