0

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?

2
  • 1
    Please update your question with the code you have tried. Commented May 24, 2022 at 8:54
  • Please add a minimal reproducible example to your question and specify exactly what it is that is not working. The task should be rather simple: parse the json, loop on its keys and on the numbers from 0 to N (in your case N=1) and extract obj[k][str(i)]. Commented May 24, 2022 at 8:57

2 Answers 2

1

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",
    },
}
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.