I have a xlsx file
Country name | Country code |
---|---|
IN | India |
SL | Sri Lanka |
I want to convert this to a json in the format
json = {
{"Name":"India",
"Code":"IN"},
{"Name":"Sri Lanka",
"Code":"SL"}
}
I tried load the excel file using the pandas and convert them to json but i am getting
json = {
"India":"IN",
"Sri Lanka":"SL"
}
2
Answers
You could use
pandas
to accomplish this. First you can read the xlsx file into a DataFrame using pandas.read_excelthen you can write that DataFrame back out as json using pandas.DataFrame.to_json
or if you want it as a json string instead of writing to file
Note that to achieve the output format you described you want to pass
orient='records'
try:
df.to_json(orient="records")