Home > front end >  json to csv python3 using pandas keys as headers
json to csv python3 using pandas keys as headers

Time:06-21

Hey

i used a dummy api generating Json products for me and i want to convert it to CSV file using Python Code

as you can see this the python code above

Output

Output

and what i want is Keys to be headers

From: {'id': 2, 'title': 'iPhone X', 'description':}

To :

Id  Title     description
1  iPhoneX  descriptionx
2   iPhoneX  descriptionx

CodePudding user response:

The snippet r.json() actually returns a dictionary with 4 keys: products, total, skip and limit. It seems that you're only interested in products, so simply modifying the program to add ["products"] after r.json() is sufficient:

import requests
import pandas as pd

r = requests.get("https://dummyjson.com/products")
json_products = pd.DataFrame(r.json()["products"])
json_products.to_csv()
print(json_products)

This outputs:

    id                                   title  ...                                          thumbnail                                             images
0    1                                iPhone 9  ...  https://dummyjson.com/image/i/products/1/thumb...  [https://dummyjson.com/image/i/products/1/1.jp...
1    2                                iPhone X  ...  https://dummyjson.com/image/i/products/2/thumb...  [https://dummyjson.com/image/i/products/2/1.jp...
2    3                      Samsung Universe 9  ...  https://dummyjson.com/image/i/products/3/thumb...   [https://dummyjson.com/image/i/products/3/1.jpg]
3    4                                 OPPOF19  ...  https://dummyjson.com/image/i/products/4/thumb...  [https://dummyjson.com/image/i/products/4/1.jp...
4    5                              Huawei P30  ...  https://dummyjson.com/image/i/products/5/thumb...  [https://dummyjson.com/image/i/products/5/1.jp...
5    6                             MacBook Pro  ...  https://dummyjson.com/image/i/products/6/thumb...  [https://dummyjson.com/image/i/products/6/1.pn...
6    7                     Samsung Galaxy Book  ...  https://dummyjson.com/image/i/products/7/thumb...  [https://dummyjson.com/image/i/products/7/1.jp...
7    8              Microsoft Surface Laptop 4  ...  https://dummyjson.com/image/i/products/8/thumb...  [https://dummyjson.com/image/i/products/8/1.jp...
8    9                          Infinix INBOOK  ...  https://dummyjson.com/image/i/products/9/thumb...  [https://dummyjson.com/image/i/products/9/1.jp...
...

CodePudding user response:

i guess this is the answer

import requests
import pandas as pd
import json
wantedElements = ["id", "description", "title"]
r = requests.get("https://dummyjson.com/products").text
all_json_products = json.loads(r)['products']
result = ""
for i in all_json_products:
    json_products = i
    for key in json_products.keys():
        if key in wantedElements:
            result  = " " key " "
    result  = "\n"
    for ele in json_products:
        if ele in wantedElements:
            result  = " " str(json_products[ele]) " "
print(result)

we don't need pandas to do this.

  • Related