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)
如果您需要幫助,您需要發佈[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)你試過的代碼 – CAB
更新了代碼 – user3646519