B4J Question JSONGenerator issue

Multiverse app

Active Member
Licensed User
Longtime User
So, I am trying to send a post with JSON string, which is generated as follows:

B4X:
'{
'  "writes": [
'    {
'      "update": {
'        "name": "projects/fall-detection-deep-learning/databases/(default)/documents/users/1",
'        "fields": {
'          "key 1": {
'            "stringValue": "String val 1"
'          }
'        }
'      }
'    }
'  ]
'}

    'Request
    Dim request As String= _
     "https://firestore.googleapis.com/v1/projects/fall-detection-deep-learning/databases/(default)/documents:commit?prettyPrint=true&key=" _
      &Token
      
    Dim name=$"projects/fall-detection-deep-learning/databases/(default)/documents/${Collections}/${Document}"$ As String
      
    'Add JSON
    Dim json As JSONGenerator
    
    
    Dim mapWrite As Map=CreateMap("update": _
     CreateMap("name": name, _
      "fields": CreateMap("fall": DateTime.Now)))
      
    Dim lstWrites As List:lstWrites.Initialize
    lstWrites.Add(mapWrite)

    
    
    Dim mapData=CreateMap("writes": lstWrites) As Map
    json.Initialize(mapData)
    Log("Data to send:")
    Log(json.ToString)


so the actual JSON that is generated is:
B4X:
{"writes":[{"update":{"name":"projects\/fall-detection-deep-learning\/databases\/(default)\/documents\/users\/1","fields":{"fall":1580222596745}}}]}

With additional "\" forward slash at the end of each "/"

Why does this happen? Is this a formatting issue?
 

Multiverse app

Active Member
Licensed User
Longtime User
How to solve this?
I get ResponSeError when used this JSON:


B4X:
ResponseError. Reason: Bad Request, Response: {
  "error": {
    "code": 400,
    "message": "Invalid value at 'writes[0].update.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), 1580231536493",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "writes[0].update.fields[0].value",
            "description": "Invalid value at 'writes[0].update.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), 1580231536493"
          }
        ]
      }
    ]
  }
}
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
is your 1580231536493 not supposed to be a string? the template seems to indicate so... it looks like the error message is pointing to the value with the backslashes, but i think it's misleading. (escaping a slash with a backslash is correct json). check your json template again up where it says
"String val 1". i think that's where your 1580231536493 goes, but as a string value, if i'm reading correctly.
 
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
is your 1580231536493 not supposed to be a string? the template seems to indicate so... it looks like the error message is pointing to the value with the backslashes, but i think it's misleading. (escaping a slash with a backslash is correct json). check your json template again up where it says
"String val 1". i think that's where your 1580231536493 goes, but as a string value, if i'm reading correctly.
This does make sense.
Although I replaced Datetime.now() with "Test", it still returns an error:

B4X:
Data to send:
{"writes":[{"update":{"name":"projects\/fall-detection-deep-learning\/databases\/(default)\/documents\/users\/4","fields":{"fall":"Test"}}}]}
ResponseError. Reason: Bad Request, Response: {
  "error": {
    "code": 400,
    "message": "Invalid value at 'writes[0].update.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"Test\"",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "writes[0].update.fields[0].value",
            "description": "Invalid value at 'writes[0].update.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"Test\""
          }
        ]
      }
    ]
  }
}

It is the firestore rest API that I am using btw https://firebase.google.com/docs/firestore/use-rest-api
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
why don't you just put "1580231536493" as a string like it wants? (or some variant of datetoString()). it seems to want a time stamp as a string. not "Test",
which is a string, yes, but not a time stamp. try giving it what it wants. if i implied any string would do, i apologize. i meant a string value in the context of what it wanted (which, apparently, is a time stamp).
 
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
Thanks for the follow-through.
or some variant of datetoString()
Nah, it accepts any string (actually, any data type). This was just my use case.

Although your reply pointed me in the right direction.
I changed the value of "fall" to "stringValue", now it works

1580301075790.png
 
Upvote 0
Top