首页 > python删除json对象

python删除json对象

如有:

{
    took: 7,
    _shards: {
        failed: 0
    },
    hits: {
        total: 5,
        hits: [
        {
            a:"del"
        },
        {
            a:"del"
        },
        {
            a:"aa"
        },
        ]
    }
}

删除a:del的集合


首先你发出来的在python就是不是一个标准的json串,需要转换

import demjson

str = """{
    took: 7,
    _shards: {
        failed: 0
    },
    hits: {
        total: 5,
        hits: [
        {
            a:"del"
        },
        {
            a:"del"
        },
        {
            a:"aa"
        },
        ]
    }
}"""

data = demjson.decode(str)
data['hits']['hits'] = [_ for _ in data['hits']['hits'] if _['a'] != 'del']
print data
【热门文章】
【热门文章】