[英]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.