Sublime Forum

How to include JSON syntax highlighting in a JS file?

#1

I have set up syntax coloring for JSON files in my theme.

However, we also use JSON notation in a JS file, which apparently does not have the scope hooks to color its code properly; it’s all “string.quoted.double.js”. See attached comparison between JS and JSON files.

How can I get JS file to use the JSON syntax definitions?

0 Likes

#2

That is difficult. There is no real way to distinguish a JSON structure in a Javascript file from a normal Javascript object. Maybe if you parsed the whole object, then you would know, but that is not how syntax highlighting works in Sublime. Javascript objects can contain functions and other things that would break the JSON format.

The way Javascript is, doesn’t lend itself to easily be highlighted like you desire.

0 Likes

#3

If your file names follows a pattern then would be easy to change the syntax when the file is opened or saved. You may also sniff a portion of the content, such if starts with "{ “content”:… "

0 Likes