简体   繁体   中英

Merging JSON files with alteration

I have a process which generates multiple JSON files. These files look something like:

{
    "_type": "TypeDict",
    "data": {
        "record": "my_record_001",
        "field1": "",
        "field2": "",
        ...
       }
}

There are about 100 of these JSON files and all of them have a unique "record" (2nd level)

I am writing a program which takes all these 100 JSON files, and rebuilds them while taking "record" out of the JSON, and using it as the key for the entire JSON.

eg If i gave it 2 JSON files, one 'my_record_001' and 'my_record_002' it should output a single JSON like:

{
    my_record_001:{
        "_type": "TypeDict",
         "data": {
        "field1": "",
        "field2": "",
        ...
       },
    my_record_002:{
        "_type": "TypeDict",
         "data": {
        "field1": "",
        "field2": "",
        ...
       },
}

Any ideas on what is the most efficient way to make this happen?

You can try something like this

records = {}

for index, jsonfile in enumerate(file_list):  # your json file list
    records["my_record_"+str(index).zfill(3)] = json.loads(jsonfile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM