0

I have spent lot of time on this and finally decided to post it here. Problem : I have a csv file I want to upload it on firestore.

  1. I convert csv to json using json lib in python3
  2. Upload that json into firestore
from urllib.request import urlopen                 #No issue
import csv                                         #No issue
import json                                        #No issue
import codecs                                      #No issue

db = firestore.Client()                            #No issue
def firestore_upload(event, context):              #No issue
    url = event['mediaLink']                       #No issue
    print(url)                                     #No issue
    csvFile = urlopen(url)                         #No issue
    csvReader = csv.DictReader(codecs.iterdecode(csvFile, 'utf-8'))   #No issue

    for line in csvReader:                         #No issue
        jsonline = json.dumps(line)                #No issue
        print(jsonline)                            #No issue
        db_collection_document = db.collection(u'mlacademyRSA').document(None) #No issue
        print('inserting record')                  #No issue
        db_collection_document.set(jsonline)       #Fails
        db_collection_document.set({"ProductID": "00000001"}) # works fine when i try to put raw json data instead of jsonline

I am not able to add jsonline variable into a collection in firestore. But I am able to put raw json data /object into the same collection.

In short line in my code i.e db_collection_document.set({"ProductID": "00000001"}) works fine but not db_collection_document.set(jsonline).

I have checked the validity of json data in jsonline by print jsonline and pasting that output into various json validators they all say its in valid json format. Pls help.

Thanks xytiz

Pavan
  • 1
  • 3
  • solution found in https://stackoverflow.com/questions/34600003/converting-json-to-string-in-python – Pavan Apr 30 '20 at 19:10
  • output that firestore likes : ```jsonline = json.loads(json.dumps(line))``` {'ProductID': '00000005', 'Category': 'Book', 'Products': 'DS on GCP', 'QoH': '50'} vs what i was trying to do firestore does not like the following format : ```jsonline = json.dumps(line)``` {"ProductID": "00000005", "Category": "Book", "Products": "DS on GCP", "QoH": "50"} It turns out that the set method takes python object and i was passing string set method in firestore takes only python object and i was passing a string – Pavan Apr 30 '20 at 19:10
  • this post also has good information : https://stackoverflow.com/questions/4162642/single-vs-double-quotes-in-json – Pavan Apr 30 '20 at 19:17

0 Answers0