0

Having data in below json format

{"A": {"Show only": ["Buy"], "Apple MacBook Product Line": ["MacBook Pro", "MacBook Air", "MacBook (Original)"], "Color": ["Space Gray", "Silver", "Gold", "Rose Gold", "Gray", "White", "Champagne Gold"], "Seller": ["Back Market", "Best Buy", "eBay"]}, "B": {"Show only": ["Buy"], "Material": ["Human Hair", "Synthetic"], "Features": ["Lace Front"], "Seller": ["arabellahair", "Ergode", "PartyBell.com"]}}

tried using below to convert this to csv format

import pandas as pd
import csv
import glob


for file in glob.glob('file.json'):
    dfjson = pd.read_json(file,encoding='utf-8',lines= False, dtype=str)
dfjson.to_csv("output.csv",index = True)

But the expected output is

A   Show only   Buy
A   Apple MacBook Product Line_001  MacBook Pro
A   Apple MacBook Product Line_002  MacBook Air
A   Apple MacBook Product Line_003  MacBook (Original)
A   Color_001   Space Gray
A   Color_002   Silver
A   Color_003   Gold
A   Color_004   Rose Gold
A   Color_005   Gray
A   Color_006   White
A   Color_007   Champagne Gold
A   Seller_001  Back Market
A   Seller_002   Best Buy
A   Seller_003  eBay
B   Show only   Buy
B   Material_001    Human Hair
B   Material_002    Synthetic
B   Features    Lace Front
B   Seller_001  arabellahair
B   Seller_002  Ergode
B   Seller_003  PartyBell.com

What changes can be made to get this output

2
  • your dict is so not fit for that Commented Apr 20, 2021 at 11:11
  • @Yefet, how must the dict be to get this output. any suggestion would be helpful Commented Apr 20, 2021 at 11:33

1 Answer 1

1

You got wrong json structure for that , here i tried replace your json with right format

json_file =  {"col1" :["A" , "A" , "A"] ,
              "col2": ["Show only" ,"Apple MacBook Product" ,"Apple MacBook Product"],
              "col3":["Buy","MacBook Pro","MacBook (Original)" ]}

df = pd.DataFrame(json_file)

df

    col1    col2           col3
0   A       Show only      Buy
1   A       Apple MacBook  Product  MacBook Pro
2   A       Apple MacBook  Product  MacBook (Original)
Sign up to request clarification or add additional context in comments.

2 Comments

is this the only possible dict structure for this output
@Vj_x49x this is the most appropriate for your task without realy much extra steps

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.