0

My data with multiple pages looks like this I want to convert it to a JSON file like below.

{
"Name" : "A",
  {
  "Project" : "P1",
  {
  [
  "T1" : "P1.com",
  ]
  },
  "Project" : "P2",
  {
  [
   "T2" : "P2.com",
   "T3" : "P2.com",   
   ]
  }
  },
 "Name" : "B",
  {
  "Project" : "Q1",
  {
  [
  "T1" : "Q1.com",
  ]
  },
  "Project" : "Q2",
  {
  [
   "T2" : "Q2.com" 
   ]
  }
  },
 "Name" : "C",
  {
  "Project" : "R1",
  {
  [
  "T1" : "R1.com",
  ]
  },
  "Project" : "R2",
  {
  [
   "T2" : "R2.com" 
   ]
  }
  }
  }

This is the first time i'm working on json files and bit confused how to make the excel's heading as key and remaining datas as values. I am able to read the excel file using pandas. Can anyone help me with the idea to do this?

import json
import pandas as pd

df = pd.read_excel (r'D:\example.xlsx', sheet_name='Sheet1')

Names = df["Name"]
d = {}

for index, name in enumerate(Names):

I have tried reading the file.Since im working with JSON files for the first time i don't have much idea about how to convert this,

2
  • 1
    pandas has a to_json method, see the docs Commented Nov 17, 2022 at 9:14
  • Please edit your JSON file to be easier to understand. @Anoosha Commented Nov 30, 2022 at 8:40

1 Answer 1

2

You have many options, I'll provide you with them using the pandas library, choose which one is more suitable to you

Example Code:

import pandas as pd

data = [
    ["A", "P1", "T1", "P1.com"],
    ["A", "P2", "T2", "P2.com"],
    ["A", "P2", "T3", "P2.com"],
    ["B", "Q1", "T1", "Q1.com"],
    ["B", "Q2", "T2", "Q2.com"],
    ["C", "R1", "T1", "R1.com"],
    ["C", "R2", "T2", "R2.com"],
]
cols = ['name', 'project', 'type', 'link']
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=cols)

In order to convert it to JSON apply one of the following:

parsed_split = json.loads(df.to_json(orient="split"))
parsed_records = json.loads(df.to_json(orient="records"))
parsed_index = json.loads(df.to_json(orient="index"))
parsed_columns = json.loads(df.to_json(orient="columns"))
parsed_values = json.loads(df.to_json(orient="values"))
parsed_table = json.loads(df.to_json(orient="table"))

If none of the following meets your requirement, for sure you'll be able to loop over one of them in order to achieve your solution, if you're struggling with that, comment and I'll update it for you.

Keep us updated, @Anoosha


Update 1: Multi Sheet Excel

import pandas as pd

sheets = ["A", "B"]
xls = pd.ExcelFile('path_to_file.xls')
for sheet in sheets:
    df = pd.read_excel(xls, sheet) # Or use without loop if you want
Sign up to request clarification or add additional context in comments.

3 Comments

hi @Lidor Eliyahu Shelef, i have managed to convert to a json using pandas. Like you said i used for loop. I tried for a simple excel sheet like the one i have attached in the question. I have a mutli paged excel. and the "Name" field is the sheet name. and there are 5 "Type" from which i should only consider data of 3(T1,T2,T3). I am not able to attach the picture in this comment. So i'll edit the main question and attach the picture there. can you please guide me on how to read the next pages and how to only consider the required column.
I added ot my answer a multi page sheet read code line, but in order for that I'll need to to edit the json you want, because the json example you brought is not a valid json file mate, can you try to focus better on what is the result you are hoping to get? @Anoosha
@Anoosha if this answer was enough, make sure to accept it or comment further problems

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.