I have a javascript object called blocki that I want to serialize and update using a rest API. So I call:
JSON.stringify(blocki)
And that gives me this string:
"{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}"
That is almost what I need, except the doubly quoted string should have single quotes on the outside, like so:
'{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}'
According to examples on MDN JSON.stringify it should return an string wrapped in single quotes. But when I try the same example in that page, I get string wrapped in double quotes. For instance, when I type JSON.stringify({}) in Firefox and chrome console, I get back "{}" instead of '{}'.
How can I properly serialize my Javascript object so the outer quotes are: '. Again, this string is an example of what I want to achieve:
'{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}'
Ideally, I would like to learn an nice way to serialize the object instead of having to modify the string after serialization.
Edit:
The reason I think I need to do this is that the API I am working with is somehow not happy when the string is wrapped in double quotes. For example when I do
curl -i -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d "{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}" 'http://localhost:3000/api/blockies/17'
The request fails and server gives a parsing error. However when I try:
curl -i -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d '{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}' 'http://localhost:3000/api/blockies/17'
The put request goes through successfully and the object is updated.
There are no differences between strings wrapped in single or double quotes, besides escaping which is done automatically by the
JSON.stringifymethod. The single/double quotes which wrap string literals are not part of the string itself.Double quotes is the way Firefox and Chrome prefer to represent string literals in the console.
Edit: Now with the CURL command it changes the meaning of the question completely.
The string above is not a valid string as you can’t have unescaped double quotes inside a double quote-wrapped string.