2016-04-25 64 views
2

如何將JSON數據從input.json轉換爲output.json using Python?一般來說,用什麼數據結構來過濾JSON數據?如何使用Python過濾JSON數據?

文件:input.json

[ 
{ 
    "id":1, 
    "a":22, 
    "b":11 
}, 
{ 
    "id":1, 
    "e":44, 
    "c":77, 
    "f":55, 
    "d":66 
}, 
{ 
    "id":3, 
    "b":11, 
    "a":22 
}, 
{ 
    "id":3, 
    "d":44, 
    "c":88 
} 
] 

文件:output.json

[ 
{ 
    "id":1, 
    "a":22, 
    "b":11, 
    "e":44, 
    "c":77, 
    "f":55, 
    "d":66 
}, 
{ 
    "id":3, 
    "b":11, 
    "a":22, 
    "d":44, 
    "c":88 
} 
] 

任何指針將不勝感激!

+0

你有沒有考慮詞典? :d – Adib

回答

3

的想法是:

執行:

import json 
from collections import defaultdict 

# read JSON data 
with open("input.json") as input_file: 
    old_data = json.load(input_file) 

# regroup data 
d = defaultdict(dict) 
for item in old_data: 
    d[item["id"]].update(item) 

# write JSON data 
with open("output.json", "w") as output_file: 
    json.dump(list(d.values()), output_file, indent=4) 

現在output.json將包含:

[ 
    { 
     "d": 66, 
     "e": 44, 
     "a": 22, 
     "b": 11, 
     "c": 77, 
     "id": 1, 
     "f": 55 
    }, 
    { 
     "b": 11, 
     "id": 3, 
     "d": 44, 
     "c": 88, 
     "a": 22 
    } 
] 
3
from collections import defaultdict 

input_list=[{"id":1, ...}, {...}] 

result_dict=defaultdict(dict) 
for d in input_list: 
    result_dict[d['id']].update(d) 

output_list=result_dict.values() 

result_dict是它採用了dict對於沒有可用的按鍵每次訪問default dictionary。因此,我們遍歷input_list並使用等於id的鍵更新我們的result_dict與來自相應字典的新值。

輸出列表是result_dict的變換,只使用其值。使用json module直接使用json數據。