python - How to merge two JSON file with pandas -
i'm trying python script merge 2 json files example:
first file: students.json
{"john smith":{"age":16, "id": 1}, ...., "paul abercom":{"age":18, "id": 764}}
second file: teacher.json
{"agathe magesti":{"age":36, "id": 765}, ...., "tom ranliver":{"age":54, "id": 801}}
so in first time, not lose informations modify files add status of each person that:
{"john smith":{"age":16, "id": 1, "status":"student"}, ...., "paul abercom":{"age":18, "id": 764, "status":"student"}} {"agathe magesti":{"age":36, "id": 765, "status":"teacher"}, ...., "tom ranliver":{"age":54, "id": 801, "status":"teacher"}}
to did following code:
import pandas pd type_student = pd.read_json('student.json') type_student.loc["status"] = "student" type_student.to_json("teststudent.json") type_teacher = pd.read_json('teacher.json') type_teacher.loc["status"] = "teacher" type_teacher.to_json("testteacher.json") open("teststudent.json") data_file: data_student = json.load(data_file) open("testteacher.json") data_file: data_teacher = json.load(data_file)
what want merge data_student , data_teacher , print resulting json in json file, can use standard library, pandas, numpy , scipy.
after tests realize teacher students can problem merge.
it looks json files contain "objects" top-level structures. these map python dictionaries. should easy using python. update first dictionary second.
import json open("mel1.json") fo: data1 = json.load(fo) open("mel2.json") fo: data2 = json.load(fo) data1.update(data2) open("melout.json", "w") fo: json.dump(data1, fo)
Comments
Post a Comment