Sublime Forum

How-to: Support single quotes on JSON syntax

#1

If you’re searching for this, you know how frustrating it is: Sublime doesn’t recognize valid JSON with single quotes. I looked, didn’t find anything… and just decided to make a fix myself (and it was really simple).

Go to your Packages folder in the main Sublime folder.
You’ll see a “JavaScript” folder. Open that.
Scroll to JSON.tmLanguage, and open that (in sublime is fine).

Do a search for string, it should be around line 217.
Copy from this line:
string



(ending line should be around 264)

Then paste it immediately afterwards.
Change the to string2.

Inside of string2’s you’ll see begin with a " following it. Change that double quote " to single quote '.
Do the same on end.
Then look down until you see patterns, you’ll find a match with some fancy regex.
One of those lines is "\ … (etc.), change that double quote to single quote.

Hard part is done.
Do a search for #string and duplicate the 2 's you find and change the duplicate #string to #string2

Done! Enjoy support for both double and single quotes in json :smile:

P.S. You can name the for string2’s from string.quoted.double.json to something like string.quoted.single.json, but you will need to then add support in your theme files to color it. It seems to work fine without changing it though (I didn’t change it). :smile:

0 Likes

#2

Sorry to break it to you, but the JSON syntax only allows double quoted strings.

Javascript and JSON are not the same thing. If you happen to have a JSON parser that allows single quotes, it does not mean that JSON allows single quotes, just that the parser is not coded to specifications.

0 Likes