Skip to main content

A library to validate and populate JSON files in Firestore.

Project description


FireJSON

A Python library for validating JSON files and populating them into Google Firestore collections.


Installation

Install the library using pip:

pip install firejson

Features

Validate JSON: Ensure JSON files conform to Firestore's structure and requirements.

Load JSON Files: Read JSON files and prepare them for Firestore population.

Populate Firestore: Insert JSON data into Firestore collections.

Logging: Comprehensive logging for success and error tracking.


Example Usage

Below is an example of how to use the library:

  1. Prepare Your Service Account Key

Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to your Firestore service account key:

export GOOGLE_APPLICATION_CREDENTIALS="path/to/service_account_key.json"

Alternatively, you can set it in your script:

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/service_account_key.json"
  1. Sample Code
from firejson.core import populate_firestore
from firejson.logging_utils import set_logger

# Initialize a logger (optional)
logger = set_logger()

# Path to the JSON file
file_path = "path/to/example_data.json"

# Firestore collection name
collection_name = "example_collection"

try:
    # Populate Firestore with the JSON data
    populate_firestore(file_path, collection_name, logger)
except ValueError as e:
    print(f"Error: {e}")
  1. Example JSON Data

Dictionary Format

{
  "users": [
    {
      "id": "user1",
      "name": "John Doe",
      "email": "johndoe@example.com"
    },
    {
      "id": "user2",
      "name": "Jane Doe",
      "email": "janedoe@example.com"
    }
  ]
}

List Format

[
  {
    "id": "doc1",
    "field1": "value1",
    "field2": "value2"
  },
  {
    "id": "doc2",
    "field1": "value3",
    "field2": "value4"
  }
]
  1. Output

If successful, the following logs will be generated:

INFO - Starting Firestore population process...
INFO - JSON structure validated successfully!
INFO - Firestore keys validated successfully!
INFO - Document 'user1' written to collection 'users'.
INFO - Document 'user2' written to collection 'users'.
INFO - Firestore population completed: 2 documents written successfully, 0 failed.

If there's an error, such as an invalid JSON format:

Error: Invalid JSON format in file 'path/to/example_data.json': [error details]

License

This project is licensed under the MIT License. See the LICENSE file for details.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

firejson-0.1.3.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file firejson-0.1.3.tar.gz.

File metadata

  • Download URL: firejson-0.1.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for firejson-0.1.3.tar.gz
Algorithm Hash digest
SHA256 d39ea888ee9a33ea5f36e49167e9405db64fe51a066e2b45ca84942cd124739b
MD5 b47175f0c7be2451ab253c1a46e0d944
BLAKE2b-256 d2e7e27b5986f2987e658e8b134a6dd7eb8d93cdddbff55ee5d8abb165149d1c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page