Home > Enterprise >  How to split data from a json file into two json files with Python
How to split data from a json file into two json files with Python

Time:05-24

I have a json file with two sets of data and I'd like to split this one json file into two separate json file so that each file has one set of data. For example, the existing json file looks like this:

{ 
  "client_id" : {"0": "abc123", "1": "def456"},
  "client_name": {"0": "companyA", "1": "companyB"},
  "revenue": {"0": "54,786", "1": "62,754"},
  "rate" : {"0": "4", "1": "5"}
}

I'm trying to make two separate json files for entry "0" and "1" like this.

File 1

{ 
  "client_id" : "abc123",
  "client_name": "companyA",
  "revenue": "54,786", 
  "rate" : "4"
}

File 2

{ 
  "client_id" :  "def456",
  "client_name": "companyB",
  "revenue":  "62,754",
  "rate" :  "5"
}

I tried to do this using a for loop but couldn't make it work. Does anyone know how to split json files in Python?

CodePudding user response:

You can try:

import json

dct = {
    "client_id": {"0": "abc123", "1": "def456"},
    "client_name": {"0": "companyA", "1": "companyB"},
    "revenue": {"0": "54,786", "1": "62,754"},
    "rate": {"0": "4", "1": "5"},
}

tmp = {}
for k, v in dct.items():
    for kk, vv in v.items():
        tmp.setdefault(kk, {}).update({k: vv})

for i, v in enumerate(tmp.values(), 1):
    with open(f"File{i}.json", "w") as f_out:
        json.dump(v, f_out, indent=4)

This creates two files File1.json, File2.json:

{
    "client_id": "abc123",
    "client_name": "companyA",
    "revenue": "54,786",
    "rate": "4"
}

and

{
    "client_id": "abc123",
    "client_name": "companyA",
    "revenue": "54,786",
    "rate": "4"
}

EDIT: To create output dictionary:

dct = {
    "client_id": {"0": "abc123", "1": "def456"},
    "client_name": {"0": "companyA", "1": "companyB"},
    "revenue": {"0": "54,786", "1": "62,754"},
    "rate": {"0": "4", "1": "5"},
}

tmp = {}
for k, v in dct.items():
    for kk, vv in v.items():
        tmp.setdefault(kk, {}).update({k: vv})

out = {}
for i, v in enumerate(tmp.values(), 1):
    out[f"File{i}"] = v

print(out)

Prints:

{
    "File1": {
        "client_id": "abc123",
        "client_name": "companyA",
        "revenue": "54,786",
        "rate": "4",
    },
    "File2": {
        "client_id": "def456",
        "client_name": "companyB",
        "revenue": "62,754",
        "rate": "5",
    },
}

CodePudding user response:

You can use the json package to read your json file and process it in a for loop

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)

# Contains the "0", "1", ...
list_of_new_dicts = data["client_id"].keys()
new_data = {}

for key, dico in data.items():
    for num, value in dico.items():
        new_data[num][key] = value

Your new data dictionnary should look like the following:

{
  "0":{
    "client_id" : "abc123",
    "client_name": "companyA",
    "revenue": "54,786", 
    "rate" : "4"
  },
  "1":{ 
    "client_id" :  "def456",
    "client_name": "companyB",
    "revenue":  "62,754",
    "rate" :  "5"
  }
}

Then to save the file you can do something like:

with open('json_data_0.json', 'w') as outfile:
    json.dump(new_data["0"], outfile)
  • Related