How do I wrap my serialized JSON string in 'single quotes'

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.


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.