简体   繁体   English

如何根据特殊键将大型 JSON 拆分为多个较小的?

[英]How to split large JSON in multiple smaller according to special keys?

I've got a large JSON in structure like this:我有一个像这样结构的大型 JSON:

{
"objectIdFieldName": "ObjectId",
"uniqueIdField": {
    "name": "ObjectId",
    "isSystemMaintained": true
},
"globalIdFieldName": "",
"fields": [{
    "name": "IdBundesland",
    "type": "esriFieldTypeInteger",
    "alias": "IdBundesland",
    "sqlType": "sqlTypeInteger",
    "domain": null,
    "defaultValue": null
}, {
    "name": "Bundesland",
    "type": "esriFieldTypeString",
    "alias": "Bundesland",
    "sqlType": "sqlTypeNVarchar",
    "length": 2147483647,
    "domain": null,
    "defaultValue": null
}, {
    "name": "Landkreis",
    "type": "esriFieldTypeString",
    "alias": "Landkreis",
    "sqlType": "sqlTypeNVarchar",
    "length": 2147483647,
    "domain": null,
    "defaultValue": null
}, {
    "name": "Altersgruppe",
    "type": "esriFieldTypeString",
    "alias": "Altersgruppe",
    "sqlType": "sqlTypeNVarchar",
    "length": 2147483647,
    "domain": null,
    "defaultValue": null
}, {
    "name": "Geschlecht",
    "type": "esriFieldTypeString",
    "alias": "Geschlecht",
    "sqlType": "sqlTypeNVarchar",
    "length": 2147483647,
    "domain": null,
    "defaultValue": null
}, {
    "name": "AnzahlFall",
    "type": "esriFieldTypeInteger",
    "alias": "AnzahlFall",
    "sqlType": "sqlTypeInteger",
    "domain": null,
    "defaultValue": null
},... 
}],
"exceededTransferLimit": true,
"features": [{
            "attributes": {
                "IdBundesland": 5,
                "Bundesland": "Nordrhein-Westfalen",
                "Landkreis": "SK K\u00f6ln",
                "Altersgruppe": "A60-A79",
                "Geschlecht": "M",
                "AnzahlFall": 1,
                "AnzahlTodesfall": 0,
                "ObjectId": 249373,
                "Meldedatum": 1578355200000,
                "IdLandkreis": "05315",
                "Datenstand": "02.03.2021, 00:00 Uhr",
                "NeuerFall": 0,
                "NeuerTodesfall": -9,
                "Refdatum": 1604966400000,
                "NeuGenesen": 0,
                "AnzahlGenesen": 1,
                "IstErkrankungsbeginn": 1,
                "Altersgruppe2": "Nicht \u00fcbermittelt"
            }
        }, {
            "attributes": {
                "IdBundesland": 5,
                "Bundesland": "Nordrhein-Westfalen",
                "Landkreis": "SK Oberhausen",
                "Altersgruppe": "A35-A59",
                "Geschlecht": "W",
                "AnzahlFall": 1,
                "AnzahlTodesfall": 0,
                "ObjectId": 193213,
                "Meldedatum": 1578787200000,
                "IdLandkreis": "05119",
                "Datenstand": "02.03.2021, 00:00 Uhr",
                "NeuerFall": 0,
                "NeuerTodesfall": -9,
                "Refdatum": 1578787200000,
                "NeuGenesen": 0,
                "AnzahlGenesen": 1,
                "IstErkrankungsbeginn": 0,
                "Altersgruppe2": "Nicht \u00fcbermittelt"
            }
        }, {
            "attributes": {
                "IdBundesland": 11,
                "Bundesland": "Berlin",
                "Landkreis": "SK Berlin Neuk\u00f6lln",
                "Altersgruppe": "A15-A34",
                "Geschlecht": "W",
                "AnzahlFall": 1,
                "AnzahlTodesfall": 0,
                "ObjectId": 1075088,
                "Meldedatum": 1579392000000,
                "IdLandkreis": "11008",
                "Datenstand": "02.03.2021, 00:00 Uhr",
                "NeuerFall": 0,
                "NeuerTodesfall": -9,
                "Refdatum": 1579392000000,
                "NeuGenesen": 0,
                "AnzahlGenesen": 1,
                "IstErkrankungsbeginn": 0,
                "Altersgruppe2": "Nicht \u00fcbermittelt"
            }},...
        }]}

Now I want to split that JSON in seperates files which contains just one 'attributes' dict per file, like:现在我想将该 JSON 拆分为单独的文件,每个文件只包含一个“属性”字典,例如:

file1:文件 1:

{
        "attributes": {
            "IdBundesland": 5,
            "Bundesland": "Nordrhein-Westfalen",
            "Landkreis": "SK K\u00f6ln",
            "Altersgruppe": "A60-A79",
            "Geschlecht": "M",
            "AnzahlFall": 1,
            "AnzahlTodesfall": 0,
            "ObjectId": 249373,
            "Meldedatum": 1578355200000,
            "IdLandkreis": "05315",
            "Datenstand": "02.03.2021, 00:00 Uhr",
            "NeuerFall": 0,
            "NeuerTodesfall": -9,
            "Refdatum": 1604966400000,
            "NeuGenesen": 0,
            "AnzahlGenesen": 1,
            "IstErkrankungsbeginn": 1,
            "Altersgruppe2": "Nicht \u00fcbermittelt"
        }
    }

file2:文件2:

{
        "attributes": {
            "IdBundesland": 11,
            "Bundesland": "Berlin",
            "Landkreis": "SK Berlin Neuk\u00f6lln",
            "Altersgruppe": "A15-A34",
            "Geschlecht": "W",
            "AnzahlFall": 1,
            "AnzahlTodesfall": 0,
            "ObjectId": 1075088,
            "Meldedatum": 1579392000000,
            "IdLandkreis": "11008",
            "Datenstand": "02.03.2021, 00:00 Uhr",
            "NeuerFall": 0,
            "NeuerTodesfall": -9,
            "Refdatum": 1579392000000,
            "NeuGenesen": 0,
            "AnzahlGenesen": 1,
            "IstErkrankungsbeginn": 0,
            "Altersgruppe2": "Nicht \u00fcbermittelt"
        }}

I am not able to do it and found no good tutorials... I'm sure there will be a simple solution with key/value assignment.我无法做到,也没有找到好的教程......我相信会有一个带有键/值分配的简单解决方案。 All over that I want to name the single JSONs after the ObjectId which is given in the 'attributes' dict.总的来说,我想在“属性”字典中给出的 ObjectId 之后命名单个 JSON。

This is my (not working) code:这是我的(不工作)代码:

with urllib.request.urlopen("https:example-url=json") as url:
    data = json.loads(url.read().decode())

for attribute in data:
    filename = f'{attribute["ObjectId"]}.json'
    with open(filename, "w", encoding="utf-8") as writeJSON:
        json.dump(attribute, writeJSON, ensure_ascii=False)

Can somebody help?有人可以帮忙吗? Thx!谢谢!

You are pretty close.你很接近。 But instead of looping through data you need to loop through data['features'] to get list of all attributes .但不是循环遍历data您需要循环遍历data['features']以获取所有attributes列表。 Then while looping through attributes you can write its data to file:然后在循环访问attributes您可以将其数据写入文件:

for attribute in data['features']:
    filename = f'{attribute["attributes"]["ObjectId"]}.json'
    with open(filename, "w", encoding="utf-8") as writeJSON:
        json.dump(attribute, writeJSON, ensure_ascii=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一个大的 json 文件拆分成多个较小的文件 - Split a large json file into multiple smaller files 将大 JSON 文件拆分成较小的文件 - Split large JSON file into smaller files 如何根据键的值拆分大型json文件? - how to split large json file according to the value of a key? 如何将一个大 json 文件拆分为多个较小的文件? - How to split one big json file into smaller multiple files? 我如何通过多处理 python 将大型 json 文件拆分为较小的 json 文件 - How i can split large json file into smaller json files through multiprocessing python 如何将具有较大时间增量的 pandas 记录拆分为具有较小时间增量的多个记录? - How to split pandas record with one large timedelta into multiple records with smaller ones? 将大型JSON文件转换并拆分为较小的Parquet文件 - Convert and split large JSON files to smaller Parquet files 根据ID将JSON文件拆分为多个JSON? - Split JSON File into multiple JSONs according to their ID? 多个具有相同键的值,值读为以'/'分隔的字符串,如何根据其键拆分值? - Multiple values with same key, values read as a string separated by '/', how to split the values according to their keys? 将大型收藏拆分成较小的收藏? - Split large collection into smaller ones?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM