Coding Global Background
Coding Global

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

Archived a month ago
1 messages
2 members
a month ago
Open in Discord
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: ```javascript 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!

Replies (1)