2017-07-26 11 views
0

JSONファイルのキー(id)の値を更新する必要があります。値は変数idsに格納されます。キーidids(更新値)に更新できますが、JSONファイルの構造が乱雑になります。誰でも私にJSON構造を邪魔することなくそれをやり遂げる方法を提案できますか?特定のキーの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" 
     } 
    ] 
+0

「混乱した」ものは何ですか?出力jsonも貼り付けてください – shikhanshu

答えて

0

あなたはおそらく、このにチェックアウトピンチを多くのことをやっているつもりなら。 https://github.com/Baggz/Pinch

var data = { 
    user: { 
    id: '123' 
    }, 
    request: { 
    id: '456' 
    }, 
    book: { 
    id: '789' 
    } 
}; 

// Converts all ids to a number 
pinch(data, /id/, function(path, key, value) { 
    return parseInt(value); 
}); 

そのような単純なルックス。

1

あなたが変更される辞書のキー(後でシリアライズされたJSON)の順序を指していると思います。これはデフォルトでjson.load()がアンダーレイマッピングタイプとしてdictを使用するためです。

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の使用:

しかし、あなたは順序を保持辞書型にそれを変更することができますが、collections.OrderedDictと呼ばれます。ドキュメントから:

object_pairs_hookは、任意のオブジェクトリテラルの結果をペアの順序付きリストでデコードした結果として呼び出されるオプションの関数です。 dictの代わりにobject_pairs_hookの戻り値が使用されます。この機能を使用すると、キーと値のペアがデコードされる順序に依存するカスタムデコーダを実装できます(たとえば、collections.OrderedDict()は挿入順序を記憶します)。

+0

元のファイルを吹き飛ばす前に一時的な名前でファイルを移動または名前を変更する方が安全かもしれません。書き込み中に何か起こったらどうなるでしょうか? –

+0

あなたはそうです、私はc/p OPコードです。私は 'remove'の呼び出しを削除しました。 – randomir

+0

orderedDictと試しても、私は同じ結果を得ています。 – Shrikant

関連する問題