[英]Best way to send JSON data from API to Firestore with Python
I have received JSON data with results for an Amazon search through an API, and i am trying to send it to my firestore database.我已收到 JSON 数据,其中包含通过 API 进行的亚马逊搜索结果,我正在尝试将其发送到我的 Firestore 数据库。 I would like it to be a collection of products, that has been searched for.
我希望它是一个已经搜索过的产品集合。
This means;这表示; for each position (product) in the search result, would i like to store it as a document.
对于搜索结果中的每个 position(产品),我想将其存储为文档吗? I have made the collection manually to show how it could look like.
我已经手动制作了这个集合以展示它的外观。
I have converted the JSON into readable json with the code below.我已使用以下代码将 JSON 转换为可读的 json。
json_api = api_result.json()
def convert_json_to_dict(json_api):
with open(json_api) as JSON:
json_dict = json.load(JSON)
return json_dict
def convert_dict_to_df(json_api):
return pd.json_normalize(convert_json_to_dict(json_api))
This gives me the following data, that i would like to handle.这给了我以下数据,我想处理。 It is the metadata and the two first positions.
它是元数据和前两个位置。
{'request_info': {'success': True,
'credits_used': 46,
'credits_remaining': 54,
'credits_used_this_request': 1},
'request_metadata': {'created_at': '2021-02-25T10:17:35.565Z',
'processed_at': '2021-02-25T10:17:39.793Z',
'total_time_taken': 4.23,
'amazon_url': 'https://www.amazon.com/s/?k=beetle+chair&ref=nb_sb_noss_2'},
'request_parameters': {'amazon_domain': 'amazon.com',
'type': 'search',
'search_term': 'beetle chair'},
'search_results': [{'position': 1,
'title': 'CosmoLiving by Cosmopolitan Astor Dining Chair Green',
'asin': 'B07P6T73Y3',
'link': 'https://www.amazon.com/dp/B07P6T73Y3',
'categories': [{'name': 'All Departments', 'id': 'search-alias=aps'}],
'image': 'https://m.media-amazon.com/images/I/81F4zscz2OL._AC_UL320_.jpg',
'is_prime': True,
'is_amazon_fresh': False,
'is_whole_foods_market': False,
'rating': 4.1,
'ratings_total': 232,
'sponsored': False,
'prices': [{'symbol': '$',
'value': 99.99,
'currency': 'USD',
'raw': '$99.99'}],
'price': {'symbol': '$',
'value': 99.99,
'currency': 'USD',
'raw': '$99.99'},
'delivery': {'tagline': 'Get it as soon as Fri, Mar 5',
'price': {'raw': 'FREE Shipping by Amazon',
'symbol': '$',
'currency': 'USD',
'value': 0,
'is_free': True}}},
{'position': 2,
'title': 'Guyou Modern Plush Velvet Accent Chairs, Upholstered Round Back Dining Chairs Living Room Decor Set of 2 with Brass Legs (Emerald Green)',
'asin': 'B085NKFJ2Y',
'link': 'https://www.amazon.com/dp/B085NKFJ2Y',
'categories': [{'name': 'All Departments', 'id': 'search-alias=aps'}],
'image': 'https://m.media-amazon.com/images/I/51Evpn0iw-L._AC_UL320_.jpg',
'is_prime': False,
'is_amazon_fresh': False,
'is_whole_foods_market': False,
'rating': 4.5,
'ratings_total': 38,
'sponsored': False,
'prices': [{'symbol': '$',
'value': 159.83,
'currency': 'USD',
'raw': '$159.83'}],
'price': {'symbol': '$',
'value': 159.83,
'currency': 'USD',
'raw': '$159.83'},
'delivery': {'price': {'raw': 'FREE Shipping',
'symbol': '$',
'currency': 'USD',
'value': 0,
'is_free': True}}},
The intention is to use this on thousands of products.目的是在数千种产品上使用它。
You could create a for loop to create a document for each search_result
and use the asin for the document name.您可以创建一个 for 循环来为每个
search_result
创建一个文档,并使用 asin 作为文档名称。
Using your data set i had to fix the data set you showed because it was invalid JSON, here is the data set for testing:使用您的数据集,我必须修复您显示的数据集,因为它是无效的 JSON,这是用于测试的数据集:
data = {'request_info': {'success': True,
'credits_used': 46,
'credits_remaining': 54,
'credits_used_this_request': 1},
'request_metadata': {'created_at': '2021-02-25T10:17:35.565Z',
'processed_at': '2021-02-25T10:17:39.793Z',
'total_time_taken': 4.23,
'amazon_url': 'https://www.amazon.com/s/?k=beetle+chair&ref=nb_sb_noss_2'},
'request_parameters': {'amazon_domain': 'amazon.com',
'type': 'search',
'search_term': 'beetle chair'},
'search_results': [{'position': 1,
'title': 'CosmoLiving by Cosmopolitan Astor Dining Chair Green',
'asin': 'B07P6T73Y3',
'link': 'https://www.amazon.com/dp/B07P6T73Y3',
'categories': [{'name': 'All Departments', 'id': 'search-alias=aps'}],
'image': 'https://m.media-amazon.com/images/I/81F4zscz2OL._AC_UL320_.jpg',
'is_prime': True,
'is_amazon_fresh': False,
'is_whole_foods_market': False,
'rating': 4.1,
'ratings_total': 232,
'sponsored': False,
'prices': [{'symbol': '$',
'value': 99.99,
'currency': 'USD',
'raw': '$99.99'}],
'price': {'symbol': '$',
'value': 99.99,
'currency': 'USD',
'raw': '$99.99'},
'delivery': {'tagline': 'Get it as soon as Fri, Mar 5',
'price': {'raw': 'FREE Shipping by Amazon',
'symbol': '$',
'currency': 'USD',
'value': 0,
'is_free': True}}},
{'position': 2,
'title': 'Guyou Modern Plush Velvet Accent Chairs, Upholstered Round Back Dining Chairs Living Room Decor Set of 2 with Brass Legs (Emerald Green)',
'asin': 'B085NKFJ2Y',
'link': 'https://www.amazon.com/dp/B085NKFJ2Y',
'categories': [{'name': 'All Departments', 'id': 'search-alias=aps'}],
'image': 'https://m.media-amazon.com/images/I/51Evpn0iw-L._AC_UL320_.jpg',
'is_prime': False,
'is_amazon_fresh': False,
'is_whole_foods_market': False,
'rating': 4.5,
'ratings_total': 38,
'sponsored': False,
'prices': [{'symbol': '$',
'value': 159.83,
'currency': 'USD',
'raw': '$159.83'}],
'price': {'symbol': '$',
'value': 159.83,
'currency': 'USD',
'raw': '$159.83'},
'delivery': {'price': {'raw': 'FREE Shipping',
'symbol': '$',
'currency': 'USD',
'value': 0,
'is_free': True}}}]}
With this sample you could do:使用此示例,您可以执行以下操作:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
# Use a service account
cred = credentials.Certificate('path/to/serviceAccount.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
for result in data["search_results"]:
doc_ref = db.collection(u'amazon').document(result["asin"])
doc_ref.set(result)
This would create a document using the asin, but it could be any other id you want or even the position on search result by using db.collection(u'amazon').document("position: " + str(result["position"]))
这将使用 asin 创建一个文档,但它可以是您想要的任何其他 id,甚至可以是 position 使用
db.collection(u'amazon').document("position: " + str(result["position"]))
Here is how'd it look if you used asin for the document name.如果您使用 asin 作为文档名称,这就是它的外观。
Here is more information on quick starting python firestore by setting up a service account and some code snippets以下是有关通过设置服务帐户和一些代码片段快速启动 python firestore 的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.