根据另一个列表过滤掉JSON对象中的项目

时间:2017-07-16 12:11:39

标签: python json

我有一个与此类似的json文件

filename = 'data.json'
with open(filename, 'r') as f:
    data = json.load(f)

data.json

    [{
        "id":"1",
        "url":"http://",
        "Types:["online","offline" ],
        "content:[{
        "Title":"A long title"
        "body":"A long body"
        }],
        "Other":"other1"


    },
    {
        "id":"2",
        "url":"http://2",
        "Types:["online2","offline"2 ],
        "content:[{
        "Title":"A long title2"
        "body":"A long body2"
        }],
        "Other":"other2"


    },
    .
    .
    .
    {
        "id":"2000",
        "url":"http://2000",
        "Types:["online2","offline2000" ],
        "content:[{
        "Title":"A long title200"
        "body":"A long body200"
        }],
        "Other":"other2000"


    }]

我想创建一个新的json文件,如果原文中的 id 与我预期的 id 匹配。

对于simplesity,我假设我想用原始json的奇数id创建一个新的json:

[{
    "id":"1",
    "url":"http://",
    "Types:["online","offline" ],
    "content:[{
    "Title":"A long title"
    "body":"A long body"
    }],
    "Other":"other1"


},
{
    "id":"3",
    "url":"http://3",
    "Types:["online2","offline"3 ],
    "content:[{
    "Title":"A long title3"
    "body":"A long body2"
    }],
    "Other":"other3"


},
.
.
.
{
    "id":"1999",
    "url":"http://1999",
    "Types:["online1999","offline1999" ],
    "content:[{
    "Title":"A long title1999"
    "body":"A long body1999"
    }],
    "Other":"other1999"
}]

我该怎么做?

修改

这就是我尝试过的方法,我在数组中添加了我的首选ID。

js=[]
for i in data:
    #print i
    if i['id']==arr:
        js.append(i)
    else:
        continue

2 个答案:

答案 0 :(得分:1)

首先,将您的id_list转换为一组,以便快速有效地查找:

id_set = set(arr)

接下来,您可以使用filter仅保留列表中ID与列表中的ID匹配的项目。

new_data = list(filter(lambda x: x['id'] in id_set, data))

确保您的id_set元素是字符串,因为示例中的id是字符串类型。

答案 1 :(得分:0)

with open("data.json", "r+") as f:        
    original = json.load(f)
    processed = [entry for entry in original if int(entry['id']) % 2 == 1]
    f.seek(0)
    json.dump(processed, f)
    f.truncate()