I am very new to Python and am trying to convert nested JSON to CSV. Below is the Python script I am trying, but I'm not getting desired output.
import json
import pandas as pd
# Load via context manager and read_json() method
with open('employee_data1.json', 'r')as file:
# load JSON data and parse into Dictionary object
data = json.load(file)
# Load JSON as DataFrame
df = pd.json_normalize(data)
# Print Result
print(df)
# output DataFrame to CSV file
df.to_csv('employee_data.csv')
I am actually trying 2 JSONs data with the above code, getting different output for each one.
employee_data1.json:
{
"features": [
{
"candidate": {
"first_name": "Margaret",
"last_name": "Mcdonald",
"skills": [
"skLearn",
"Java",
"R",
"SQL",
"Spark",
"C++"
],
"state": "AL",
"specialty": "Database",
"experience": [
{
"company": "XYZ Corp",
"position": "Software Engineer",
"start_date": "2016-01-01",
"end_date": "2021-03-01"
},
{
"company": "ABC Inc",
"position": "Senior Software Engineer",
"start_date": "2021-04-01",
"end_date": null
}
],
"relocation": "no"
}
},
{
"candidate": {
"first_name": "Michael",
"last_name": "Carter",
"skills": [
"TensorFlow",
"R",
"Spark",
"MongoDB",
"C++",
"SQL"
],
"state": "AR",
"specialty": "Statistics",
"experience": [
{
"company": "DFC Corp",
"position": "Software Engineer",
"start_date": "2016-01-01",
"end_date": "2021-03-01"
},
{
"company": "SDC Inc",
"position": "Senior Software Engineer",
"start_date": "2021-04-01",
"end_date": null
}
],
"relocation": "yes"
}
}
]
}
employee_data2.json:
{
"features":
{
"candidate": {
"first_name": "Margaret",
"last_name": "Mcdonald",
"skills": [
"skLearn",
"Java",
"R",
"SQL",
"Spark",
"C++"
],
"state": "AL",
"specialty": "Database",
"experience": [
{
"company": "XYZ Corp",
"position": "Software Engineer",
"start_date": "2016-01-01",
"end_date": "2021-03-01"
},
{
"company": "ABC Inc",
"position": "Senior Software Engineer",
"start_date": "2021-04-01",
"end_date": null
}
],
"relocation": "no"
}
}
}
Below, I have chosen only a few fields, instead of all fields. I am expecting the below Desired output. I will be glad if someone can able to help me out on this.
candidate.first_name, candidate.last_name, candidate.skills, candidate.state, candidate.experience.company, candidate.experience.position
Margaret, Mcdonald, "['skLearn', 'Java', 'R', 'SQL', 'Spark', 'C++']", AL, XYZ Corp, Software Engineer