The JSON standard uses double quotes for key names and string values, such as the following:


However, the current project I am working on requires JSON-encoded objects to use single quotes instead of double quotes. (In this case {'one':1,'two':'two'}). Right now, there is no reasonable way to get rid of this requirement. I am using the following code to convert a JSON-encoded string from double quotes to single quotes:

var encoded = JSON.stringify(...);
encoded = encoded.replace(/\\"/g, '"')
    .replace(/([\{|:|,])(?:[\s]*)(")/g, "$1'")
    .replace(/(?:[\s]*)(?:")([\}|,|:])/g, "'$1")
    .replace(/([^\{|:|,])(?:')([^\}|,|:])/g, "$1\\'$2");

This code replaces double quotes with single quotes on key names and string values, and handles possible quote escaping issues (such as {"\"key\"": "'value'"}, which is replaced with {'"key"', '\'value\''}).

Is this code enough to guarantee that, for any given object, a valid JSON-like string with single quotes will be generated? In other words, is there any way to break this code such that the result is not valid?

P.S: if you are interested, the reason that JSON strings must have single quotes is that I am working with Cassandra's map type, which doesn't accept double quotes.

  • 5
    \$\begingroup\$ Then it is not JSON... Seems like an XY problem to me. \$\endgroup\$
    – elclanrs
    Nov 8, 2014 at 21:17
  • \$\begingroup\$ @elclanrs: I'm basically asking for a code review on "Correctness in unanticipated cases". Is there any way to improve this question? \$\endgroup\$
    – jcoc611
    Nov 8, 2014 at 21:22
  • \$\begingroup\$ @JuanCamiloOsorio that's kinda beside the point. He says you're trying to hammer a nail with a bottle, and now are desperately trying to stop the bottle from breaking everytime you hit the nail. \$\endgroup\$
    – Vogel612
    Nov 8, 2014 at 21:24
  • 1
    \$\begingroup\$ @jcoc611 and that again is not a good question to ask here ;) TBH I am not working all that much with JS and JSON, but you shouldn't have to mess with the data's inherent structure to use it. Whatever sends JSON back to you should be modified so that it sends Cassandra's map type instead ;) \$\endgroup\$
    – Vogel612
    Nov 8, 2014 at 21:29
  • 1
    \$\begingroup\$ Do you know how to read JSON into a Javascript object? If so, maybe you just need help writing Cassandra from Javascript. \$\endgroup\$
    – Brythan
    Nov 8, 2014 at 23:51

2 Answers 2


No, that code is not enough to handle any valid JSON.

Here are two examples where the result is not only different from the expected, but also unparsable:

The string:


will be converted to:


The string:


will be converted to:

  • \$\begingroup\$ Thanks, this is exactly what I was looking for. I'm currently writting a MAP parser based on some JSON 3 parsers out there \$\endgroup\$
    – jcoc611
    Nov 8, 2014 at 23:05

Disclaimer: I post this answer for reference purposes only, and note that it contains my own work. In other words, this is a form of self-advertisement for the sole sake of helping anyone who is looking for a similar solution.

The new solution I developed is based on bestiejs's JSON 3, an can be found here. It essentially is a complete implementation of a JSON-like encoder/decoder that differs from the official specification in that it uses single quotes for keys and values instead of double quotes.

Generating a CQL-compilant serialized object can be done as following:


This solution passes both tests posted by Guffa (and all of the ECMAScript 5 Conformance tests modified to be compliant for single-quoted literals, along with a bunch of other tests).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.