I am at the last straw with trying to get a local json file into my Firestore database. No matter how I format my data, I get some sort of error. I even took the example right from the google firebase instructions and still no luck. Here is what I am doing:
*Note: I am using R. I don't want to, but I have to.
- Authenticate and get my bearer token.
- Create the data in a json file (from the firestore example) and import it into R. Below is the file I am reading in.
{
"users": [
{
"id": "1",
"firstName": "Kristin",
"lastName": "Smith",
"occupation": "Teacher",
"reviewCount": "6",
"reviewScore": "5",
},
{
"id": "2",
"firstName": "Olivia",
"lastName": "Parker",
"occupation": "Teacher",
"reviewCount": "11",
"reviewScore": "5"
}
]
}
- Call the function I have for writing data:
write.db <- function(db_endpoint, data, auth_token) {
r <- PATCH(db_endpoint,
add_headers("Content-Type" = "application/json",
"Authorization" = paste("Bearer", auth_token)), body = data)
return(r)
}
- Experience the following error message:
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"users\" at 'document': Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
...
The interesting thing is, if I try to write something simpler like the below (no array of objects), I can do so with no problem:
{
"fields": {
"name": {
"stringValue": "Gabriel"
},
"favoriteNumber": {
"integerValue": "32343"
}
}
}
Can someone PLEASE explain to me what I am doing wrong here. I have tried reformatting my data a thousand different ways but nothing seems to work.
CodePudding user response:
I think you just need to expressly convert that entity to a JSON object like so:
require(jsonlite)
data=toJSON(data)
Then write it as you have tried already. When you just pass that structure surrounded with quotes it is character data, but Firestore expects JSON.
CodePudding user response:
I actually figured it out after a lot of toying with the json format. Turns out there are some fields in the json that need to be present when sending an array of json objects. My previous attempt wasn't even close to correct. I guess there are hidden fields (like arrayValue, values, mapValue) that need to be there but aren't really shown clearly when you are looking at the data in firestore. Instead, I needed something like the below and now it works (I used curl to send the data):
{
"fields": {
"my_array_name": {
"arrayValue": {
"values": [
{
"mapValue": {
"fields": {
"my_item": {
"stringValue": "value_of_my_item"
}
}
}
}
]
}
}
}
}
This is all just to populate an array like the below in firestore:
my_array_name: [{my_item: value_of_my_item}]
I hope that this is helpful to someone in the future. For the life of me I do not understand why putting in the data just in regular JSON format does not work (or at least does not work for me). I don't think this is an issue unique to me though because I tried sending regular JSON (literally the google firestore example JSON format read in from a json file) and could not PATCH the data with R, Postman, or curl. Only this format worked.