DUDANF
DUDANF

Reputation: 2990

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.

e.g. 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?

Upvotes: 0

Views: 50

Answers (1)

mohammed wazeem
mohammed wazeem

Reputation: 1328

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)

Upvotes: 1

Related Questions