Coding Global Background
Coding Global

Best way to handle sending empty data in json API call?

Archiviert 2 months ago
1 Nachrichten
2 Mitglieder
Erstellt 2 months ago
Aktualisiert 2 months ago
In Discord öffnen
D
dialoguechicken
Verified
I'm trying to post a struct to my server with a POST API call from my typescript frontend.

Attempting to send an empty string for some of my struct values causes json errors because a json value can't be empty. However, the value is empty. I bind to the value with a few different items on my web page, so I want to indicate that the value is truly empty. Replacing it with null when creating the json to send to the API call with:
let struct: MyStruct = {
  value: value || null,
}

Triggers the same error. Specifically, it'll be something like: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

What's the correct solution here? The only one I can come up with is creating a keyword placeholder value which should never be real data, and using that to indicate that the data is really blank and then handling that in the backend. I don't like that solution and I'm sure a better one exists. What's the idiomatic fix here? Thanks!