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.
Strings in JSON are specified using double quotes, i.e., " . If the strings are enclosed using single quotes, then the JSON is an invalid JSON .
JSON is standardized to use double-quotes, to the degree that this would not be considered standard JSON and would error in a JSON parser.
You don't need those single quotes wrapping the string - those are only there on the MDN page to show the string literals that correspond to the output.
The quotes are not part of the content of the strings themselves!
EDIT - you've edited the question since I wrote the above.
The simple answer is that if you absolutely must wrap the string in single quotes yourself, just use:
var json = "'" + JSON.stringify(obj) + "'"
The longer answer is still that you shouldn't be wrapping the string at all. It's considered bad practise to pass entire command lines to a shell - the presence of certain environment variables (especially IFS
) can change the way that the command line is interpreted, leading to security issues.
Since you're using Javascript I guess perhaps you're using nodejs
and the child_process
module? If so, you should be using .spawn
instead of .exec
, and passing the parameters as an array. When passed this way the parameters are passed directly into Curl's argv
array without being parsed by the shell first, and therefore need no quoting at all, e.g.:
var child = spawn('curl', [
'-i', '-H', 'Accept: application/json',
'-H', 'Content-type: application/json',
'-X', 'PUT',
'-d', json,
'http://localhost:3000/api/blockies/17'
]);
or better yet make the PUT call directly from Node without using Curl.
There are no differences between strings wrapped in single or double quotes, besides escaping which is done automatically by the JSON.stringify
method. 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.
"{"name":"Updated Blocki","bounds":{"x":"2em","y":"2em","w":"8em","h":"12em"}}"
The string above is not a valid string as you can't have unescaped double quotes inside a double quote-wrapped string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With