0

Hey

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

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

2 Answers 2

1

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...
...
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

This answer uses space separators for the .csv, then? The descriptions also contain spaces, making the .csv unreadable. Also, why for i in all_json_products: json_products = i, instead of just for json_products in all_json_products? I would recommend sticking to pandas for this one.

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.