https://github.com/captaincodeman/firestore-json
Firestore to JSON Converter
https://github.com/captaincodeman/firestore-json
Last synced: 2 months ago
JSON representation
Firestore to JSON Converter
- Host: GitHub
- URL: https://github.com/captaincodeman/firestore-json
- Owner: CaptainCodeman
- Created: 2021-01-18T23:24:44.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-27T15:28:21.000Z (almost 5 years ago)
- Last Synced: 2025-02-08T19:36:44.802Z (12 months ago)
- Language: JavaScript
- Size: 64.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Firestore-JSON
Convert Firestore REST response into simple JSON




Adapted from [firestore-parser](https://www.npmjs.com/package/firestore-parser) to reduce size, improve performance, add options to decode timestamps to dates and base64 strings to bytes, and fix some errors.
## Installation
Install using your package manager of choice:
pnpm i firestore-json
## Example
```ts
import { toJSON } from 'firestore-json'
const url = `https://firestore.googleapis.com/v1/projects/my-project/databases/(default)/documents/mycollection/my-doc`
const resp = await fetch(url)
const json = await resp.json()
const data = toJSON(json.fields)
```
## Options
By default, `timestampValue` and `bytesValue` fields are left as-is (ISO and Base64 strings respectively) but these can be converted to Javascript `Date` and `Uint8Array` types automatically by passing an options object to the `toJSON` method with `timestamps` and / or `bytes` set to true as required:
```ts
const data = toJSON(json.fields, { timestamps: true, bytes: true })
```
## Data Structure
The Firestore JSON returned in the REST API, uses value type as keys. This can be difficult to work with since you have to know the data type prior getting the value. The firestore-parser removes this barrier for you.
### JSON Response from Firestore
```json
{
"player": {
"mapValue": {
"fields": {
"name": {
"stringValue": "steve"
},
"health": {
"integerValue": "100"
},
"alive": {
"booleanValue": true
}
}
}
},
"level": {
"integerValue": "7"
}
}
```
### JSON from firestore-json toJSON
```json
{
"player": {
"name": "steve",
"health": 100,
"alive": true
},
"level": 7
}
```