更新特定键的JSON值,保留JSON结构

时间:2017-07-26 20:00:04

标签: python json

我需要更新JSON文件中密钥(id)的值。值存储在变量ids中。我能够使用id更新密钥ids(更新后的值),但JSON文件的结构搞砸了。任何人都可以建议我这样做而不会弄乱JSON结构吗?

代码:

ids=10
filename='update_test.json'
with open(filename,'r') as f:
    data=json.load(f)
    data['id'] = ids
os.remove(filename)
with open(filename,'w') as f:
    json.dump(data,f,indent=4)

输入JSON:

{
    "accountableExecutiveTech": "string",
    "api": true,
    "arrivalFrequency": "string",
    "bucketName": "string",
    "businessDataset": "string",
    "columns": [{
        "businessColumnName": "string",
        "childColumns": [{}],
        "columnOrder": 0,
        "description": "string",
        "descriptiveName": "string",
        "format": "string",
        "hierarchicalName": "string",
        "id": 0,
        "isArray": true,
        "length": 0,
        "name": "string",
        "parentColumnName": "string",
        "partitionColumn": true,
        "technicalDatasetId": 0,
        "technicalDatasetName": "string",
        "technicalNamespace": "string",
        "technicalPlatformName": "string",
        "type": "string",
        "validValues": {}
    }],
    "controlMJobName": "string",
    "credit": true,
    "delimiter": "string",
    "delimiterFlag": true,
    "description": "string",
    "dqPrioritized": true,
    "fileFormat": "string",
    "id": "",
    "name": "string",
    "namespace": "string",
    "npi": true,
    "objectKey": "string",
    "pci": true,
    "performingDataSteward": "string",
    "platformName": "string",
    "retentionPlan": "string",
    "selectAdGroup": "string",
    "sourceDatasets": [{
        "id": 4534,
        "name": "string",
        "namespace": "string",
        "platformName": "string"
    }],
    "tags": ["string"]
}

输出JSON:

{
    "accountableExecutiveTech": "string",
    "delimiterFlag": true,
    "performingDataSteward": "string",
    "api": true,
    "dqPrioritized": true,
    "id": 14044,
    "namespace": "string",
    "fileFormat": "string",
    "selectAdGroup": "string",
    "pci": true,
    "platformName": "string",
    "columns": [
        {
            "isArray": true,
            "partitionColumn": true,
            "description": "string",
            "technicalDatasetId": 0,
            "format": "string",
            "technicalPlatformName": "string",
            "parentColumnName": "string",
            "columnOrder": 0,
            "length": 0,
            "childColumns": [
                {}
            ],
            "descriptiveName": "string",
            "validValues": {},
            "technicalDatasetName": "string",
            "technicalNamespace": "string",
            "hierarchicalName": "string",
            "businessColumnName": "string",
            "type": "string",
            "id": 0,
            "name": "string"
        }
    ],
    "businessDataset": "string",
    "npi": true,
    "description": "string",
    "tags": [
        "string"
    ],
    "arrivalFrequency": "string",
    "objectKey": "string",
    "bucketName": "string",
    "controlMJobName": "string",
    "name": "string",
    "retentionPlan": "string",
    "credit": true,
    "delimiter": "string",
    "sourceDatasets": [
        {
            "platformName": "string",
            "namespace": "string",
            "id": 4534,
            "name": "string"
        }
    ]

1 个答案:

答案 0 :(得分:1)

我猜你是指你的字典中的键的顺序(后来在序列化的JSON中)被改变了。这是因为默认情况下json.load()使用dict作为底层映射类型。

但您可以将其更改为保留顺序的字典类型,称为collections.OrderedDict

from collections import OrderedDict

ids = 10
filename = 'update_test.json'

with open(filename, 'r') as f:
    data = json.load(f, object_pairs_hook=OrderedDict)
    data['id'] = ids

with open(filename, 'w') as f:
    json.dump(data, f, indent=4)

请注意json.load()object_pairs_hook=OrderedDict的使用情况。来自文档:

  

object_pairs_hook是一个可选函数,将使用对有序列表对解码的任何对象文字的结果进行调用。将使用object_pairs_hook的返回值而不是dict。此功能可用于实现依赖于键和值对解码顺序的自定义解码器(例如,collections.OrderedDict()将记住插入顺序。)