I have this json object:
values: {
username: { type: String, unique: true },
password: { type: String }
}
When running JSON.stringify(values) I get:
{"username":{"unique":true},"password":{}}
It omits type: String.
Any workarounds?
JSON.stringifyomits values that are not valid JSON, includingundefinedandFunction.If you really really want to, you could use the
replacerparameter (not sure how cross-browser this will be though) and work around this. This function will basically inspect to see if the value is a function, and it will return the function name. In your example, it will return"String", since String is a global constructor function.Although, to be clear, you’re better off using a string as your type, (
"String"instead ofString) no hoops to jump through then.