0

I am trying to upload some data by using json files through python. But I am getting this error and can't resolve. Here is the Python Code. Json files are on separate folders in the same folder. I just want to ad some categories and items to the firebase using this. Got this code from the Git because it matches with my requirement.

Code:

    '''
Uploaded  furniture dataset to firebase

'''

import json
from typing import List
import firebase_admin
from firebase_admin import credentials, storage, firestore
from os import listdir
from os.path import isfile, join

#
STORAGE_PATH = "gs://couchmirage-570ec.appspot.com"
CERTIFICATE = "C:/Users/isuru/Downloads/CouchMirageFirebaseLoader-master/CouchMirageFirebaseLoader-master/key.json"
DATA_PATH = "C:/Users/isuru/Downloads/CouchMirageFirebaseLoader-master/CouchMirageFirebaseLoader-master/TO_ADD"


def load_json(file_path):
    with open(file_path, 'r') as f:
        data = json.load(f)
    return data


def files_in_dir(mypath):
    return [join(mypath, f) for f in listdir(mypath) if isfile(join(mypath, f))]


def compare_records(doc1, doc2):
    return doc1.items() == doc2.items()


def main():
    # firebase
    cred = credentials.Certificate(CERTIFICATE)
    app = firebase_admin.initialize_app(cred, {'storageBucket': STORAGE_PATH})
    store = firestore.client()
    doc_ref = store.collection(u'BEDS & MATTRESSES')
    docs = doc_ref.get()
    bucket = storage.bucket("couchmirage-570ec.appspot.com")

    #
    path = DATA_PATH
    items_types = listdir(path)

    for item_type in items_types:
        path = DATA_PATH + '/' + item_type

        items_index = listdir(path)

        for index in items_index:
            item_files = listdir(path + '/' + index)

            images = [item_file for item_file in item_files if item_file.endswith('.jpg')]
            model = [item_file for item_file in item_files if item_file.endswith('.glb')][0]
            collection = [item_file for item_file in item_files if item_file.endswith('.json')][0]

            # check whenevrer uploaded
            exists = False

            item_data = load_json(path + '/' + index + '/' + collection)

            for doc in docs:
                if compare_records(doc._data, item_data):
                    exists = True

            if not exists:
                print("Add:" + model)
                # add record
                doc_ref.add(item_data)

                # # add model
                # fileName = path + '/' + index + '/' + model
                # blob = bucket.blob('models/' + model)
                # blob.upload_from_filename(fileName)
                #
                # # add images
                # for image in images:
                #     fileName = path + '/' + index + '/' + image
                #     blob = bucket.blob('images/' + image)
                #     blob.upload_from_filename(fileName)
            else:
                print("Skip:" + model)


if __name__ == "__main__":
    main()

Error:

**Traceback (most recent call last):
  File "c:\Users\isuru\Downloads\CouchMirageFirebaseLoader-master\CouchMirageFirebaseLoader-master\main.py", line 87, in <module>
    main()
  File "c:\Users\isuru\Downloads\CouchMirageFirebaseLoader-master\CouchMirageFirebaseLoader-master\main.py", line 61, in main
    item_data = load_json(path + '/' + index + '/' + collection)
  File "c:\Users\isuru\Downloads\CouchMirageFirebaseLoader-master\CouchMirageFirebaseLoader-master\main.py", line 21, in load_json
    data = json.load(f)
  File "C:\Python310\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Python310\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 114: character maps to <undefined>**

1 Answer 1

1

The JSON file in question is not using the CP1252 encoding. It's using another encoding. Which one you have to figure out yourself. Common ones are Latin-1 and UTF-8.

You can specify the encoding when you open the file. Try something like this:

with open(file_path, encoding='utf-8') as f:
    data = json.load(f)
Sign up to request clarification or add additional context in comments.

Comments

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.