需要从JSON文件中删除一些不必要的信息并保留JSON结构

2024-06-02 20:19:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个JSON文件

[
    {
        "api_key": "123123112313121321",
        "collaborators_count": 1,
        "created_at": "",
        "custom_event_fields_used": 0,
        "discarded_app_versions": [],
        "discarded_errors": [],
        "errors_url": "https://api.bugsnag.com/projects/1231231231312/errors",
        "events_url": "https://api.bugsnag.com/projects/1231231231213/events",
        "global_grouping": [],
        "html_url": "https://app.bugsnag.com/lol/kek/",
        "id": "34234243224224",
        "ignore_old_browsers": true,
        "ignored_browser_versions": {},
        "is_full_view": true,
        "language": "javascript",
        "location_grouping": [],
        "name": "asdasdaasd",
        "open_error_count": 3,
        "release_stages": [
            "production"
        ],
        "resolve_on_deploy": false,
        "slug": "wqeqweqwwqweq",
        "type": "js",
        "updated_at": "2020-04-06T15:22:10.480Z",
        "url": "https://api.bugsnag.com/projects/12312312213123",
        "url_whitelist": null
    }
]

我需要的是删除除“id:”和“name:”之外的所有行,并保留JSON结构。有人能建议Python或bash脚本来处理这个问题吗


Tags: httpscomapijsonappurlcountevents
3条回答

您可以尝试以下方法:

import json

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

new_data = []
for item in data:
    new_item = {key: value for key, value in item.items() if key == "id" or key =="name"}
    new_data.append(new_item)

with open('<output filename>', 'w') as f:
    json.dump(new_data, f)

jq一起:

$ jq 'map({id: .id, name: .name})' input.json 
[
  {
    "id": "34234243224224",
    "name": "asdasdaasd"
  }
]

使用python,您可以首先用^{}反序列化JSON文件(对象的JSON数组),然后用列表过滤出所需的键:

from json import load

keys = ["name", "id"]

with open("test.json") as json_file:
    data = load(json_file)

    filtered_json = [{k: obj.get(k) for k in keys} for obj in data]

    print(filtered_json)

输出:

[{'name': 'asdasdaasd', 'id': '34234243224224'}]

如果要将此python列表序列化为另一个输出文件,可以使用^{}

from json import load
from json import dump

keys = ["name", "id"]

with open("test.json") as json_file, open("output.json", mode="w") as json_output:
    data = load(json_file)

    filtered_json = [{k: obj.get(k) for k in keys} for obj in data]

    dump(filtered_json, json_output, indent=4, sort_keys=True)

output.json

[
    {
        "id": "34234243224224",
        "name": "asdasdaasd"
    }
]

相关问题 更多 >