Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove Cloud Firestore field type specifiers when using REST API?

I totally made up the name "type specifiers." What I mean is the stringValue key in front of a value. Usually I would expect a more-standard response: "name" : "name_here".

{
 "fields": {
  "name": {
   "stringValue": "name_here"
  }
 }
}

Is it possible to remove those when making a GET call?

More importantly, it be nice to understand why it's structured like it is. Even for POST-ing data? The easy answer is probably because Cloud Firestore, unlike Realtime Database, needs to know the specific types, but what are all the deeper reasons? Is there an "official" name for formatting like this where I could do more research?

For example, is the reasoning any related to Protocol Buffers? Is there a way to request a protobuf instead of JSON?

Schema: enter image description here

like image 746
kgaidis Avatar asked Aug 12 '26 22:08

kgaidis


1 Answers

Is it possible to remove those when making a GET call?

In short No. The Firestore REST API GET returns an instance of Document. See https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents#Document

{
  "name": string,
  "fields": {
    string: {
      object(Value)
    },
    ...
  },
  "createTime": string,
  "updateTime": string,
}

Regarding the "Protocol Buffer": When the data is deserialized you could just have a function to convert into the structure you wish to use, e.g. probably using the protocol buffers if you wish but as there appear to be libraries for SWIFT, OBJECTIVE-C, ANDROID, JAVA, PYTHON, NODE.JS, GO maybe you won’t need to use the REST API and craft a Protocol Buffer.

Hopefully address your “More Importantly” comment:

As you eluded to in your question Firestore has a different data model to the Realtime Database. Realtime database data model allows JSON objects with the schema and keywords as you want to define it. As you point out, the Firestore data model uses predefined schemas, in that respect some of the keywords and structure cannot be changed.

The Cloud Firestore Data Model is described here: https://firebase.google.com/docs/firestore/data-model

Effectively the data model is / where a document can contain a subcollection and the keywords “name”, “fields”, “createdTime”, “upTime” are in a Firestore document (a pre-defined JSON document schema). A successful the Firestore REST API GET request results in a Document instance which could contain collection of documents or a single document. See https://firebase.google.com/docs/firestore/reference/rest/. Also the API discovery document helps give some detail about the api: https://firestore.googleapis.com/$discovery/rest?version=v1beta1

An example REST API URL structure is of the form:

https://firestore.googleapis.com/v1beta1/projects/<yourprojectid>/databases/(default)/documents/<collectionName>/<documentID>

It is possible to mask certain fields in a document but still the Firestore Document schema will persist. See the three examples GET:

  • collection https://pastebin.com/98qByY7n
  • document https://pastebin.com/QLwZFGgF
  • document with mask https://pastebin.com/KA1cGX3k

Looking at another example, the REST API to run Queries https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/runQuery the response body is of the form:

{
  "transaction": string,
  "document": {
    object(Document)
  },
  "readTime": string,
  "skippedResults": number,
}

In summary:

The Realtime database REST API will return the JSON for the object according to the path/nodes as per your “more-standard response”.

The Firestore REST API returns a specific Firestore predefined response structure.

There API libraries available for several language so maybe it’s not necessary to use the REST API and craft your own Protocol Buffer but if you needed to you it’s probably feasible.

like image 187
Steve Avatar answered Aug 14 '26 16:08

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!