Sublime Forum

JSON syntax highlighting does not work

#1

Hello, when I open a .json-file ST automatically selects JSON syntax hightlighting, but it looks the same as no highlighting (ie only one color). But if I change the highlighting to JavaScript, it looks fine.

Any tips on how to fix this?

JSON highlighting to the left, JavaScript highlighting to the right

1 Like

#2

Looks fine to me, your JSON file only consists of strings (which are light grey in JSON syntax highlighting).
Maybe your real question is if it’s possible to give JSON the same highlighting as JavaScript, which is a little odd of course. :wink:

What colors do you get when you input numbers in your JSON file? If they turn purple (assuming you use the Monokai color scheme), then it works fine.

0 Likes

#3

Yeah, both errors and numbers are in a different color, so I guess it is working. I guess what I want is for strings to be in another color, or commas, or moustaches. Most of my json-files are strings in objects and arrays, so for me everything is always in just one color.

Do you (or anyone else) know why javascript highlights strings but JSON does not?
I actually think I will set .json-files to javascript highlighting for now even though its odd.

Cheers!

UPDATE There is a special tag for json-strings in the theme definition; “string.quoted.double.json”, so I just created a remix of the theme with another color on the json-strings.

0 Likes

#4

I found the JSON highlighting in TextWrangler (I think it is the default) useful. It differentiates between a string used as a tag-name from strings that are values. This makes JSON much easier to read in TextWrangler than Sublime. I’m new to Sublime… does the syntax description differentiate between these two types of strings (which can then be highlighted in the themes, distinctly)?

0 Likes

#5

Would you be willing to share what you have come up with? I have been searching all over for some simple JSON formatting. Does it work when JSON is auto selected?

0 Likes

#6

For anyone else who stumbles on this page wondering how to give strings in JSON a different colour…

Find your current theme file. I was using the monokai soda theme, which I found in my user packages. Open up your thTheme file. I then found the following XML (simply searched for “json”)

<dict> <key>name</key> <string>JSON String</string> <key>scope</key> <string>meta.structure.dictionary.json string.quoted.double.json</string> <key>settings</key> <dict> <key>foreground</key> <string>#CFCFC2</string> </dict> </dict>
And then I altered it to be the following. Note I’ve commented out the previous value and added the standard sublime yellow:

<dict> <key>name</key> <string>JSON String</string> <key>scope</key> <string>meta.structure.dictionary.json string.quoted.double.json</string> <key>settings</key> <dict> <key>foreground</key> <!-- <string>#CFCFC2</string> --> <string>#E6DB74</string> </dict> </dict>

Save your theme and all should then work :smile:

I even think you might be able to edit the json tmLanguage file to capture string keys differently to string values - but that’s for another day!

0 Likes

#7

Hey!

Agreed! Syntax highlighting in JSON is valuable in the to easily

  • Identify nesting levels
  • Seperate Text, Handlebars, colons and strings
  • Flag errors

Check out Monokai JSON
https://github.com/jonschlinkert/sublime-monokai-extended/blob/master/Monokai%20Extended.JSON-tmTheme

Easily installed through Package Manager. (found at https://packagecontrol.io/installation)

  1. Hit Ctrl + Sh + P
  2. Enter ‘Install Package’
  3. Type Monokai JSON+. Enter
  4. Once installed, select it as per screenshot.

http://www.dumpt.com/img/files/3p18oxbsj18mo5ykypm1_thumb.png

0 Likes

#8

I’m not seeing the Monokai JSON theme in the Package Manager. Is it still there?

0 Likes

#9

Yes, still there. The steps listed by HighGlow worked for me.

  1. Hit Ctrl + Sh + P
  2. Enter ‘Install Package’
  3. Type Monokai JSON+. Enter
  4. Once installed, select it as per screenshot.
0 Likes

#10

it does in builds released since this PR was merged (see the diff for details)

the scopes may change in future though:

1 Like