9

I would like to gain syntax highlighting support for the JSONL data format — "JSON-Lines" — as the format is not yet supported in 'V.S. Code'.

The JSONL website recommends that I search for an extension that adds support for JSON Lines, however, all attempts I made to find such an extension did not return any useful results.

I also searched Visual Studio Marketplace for "JSON Lines" and ".jsonl", but once again, the search did not return any helpful results.

At this point I am left wondering if it is possible to add support for the data format?

Mark
  • 143,421
  • 24
  • 428
  • 436
Max Heiber
  • 14,346
  • 12
  • 59
  • 97
  • 1
    To future readers: looks like someone just created [language support for JSONL](https://marketplace.visualstudio.com/items?itemName=Alpha4.jsonl). – MC Emperor May 24 '22 at 11:45

2 Answers2

7

Edit 2022:

The other two suggestions here are valid, and they could work as a possible solution, however, as of 2022, a new feature has been added to V.S. Code that allows users to inject a grammar into another grammar.

Typically, the way this works, is the person who is looking to create a language-style extension in V.S. Code (an extension that adds syntax highlighting or other language features) will find a base grammar that already exists in the VS Code Eco-system. The ideal grammars to use are the ones already built-in to the editor. The extension-developer will then inject a smaller grammar into the pre-existing grammar. Because many of the data-types, languages, API's, SDK's, RTE's etc... that are used by contemporary developers, are just sub-sets (or extensions of, if your will) injecting a grammar into a grammar makes it extremely quick, and simple to add support for syntax highlighting for many features out-their.

Like in the case of the OP, he is looking for JSON-Lines, so he would use the built-in JSON grammar, and just inject the additional syntax highlighting on top of the already existent JSON grammar.

This is typically how the process would work anyways, but you would normally have to manually copy, paste, then maintain the JSON grammar. Which sounds easy, but when you compare it to creating and maintaining, what is practically only a snippet (the injected grammar), its a huge step already completed for you.

For injecting grammars visit this link


2021-JULY-21st @ 04:08 UTC —  EDIT: "Added another possible solution"

Possible Solution for Supporting Unsupported File-Types


While answering another question that was sort of about "language-support in V.S. Code", I remembered the question from this 'Q & A', and I realized that there was a solution that may have worked. If the author has not yet found a solution, then I suggest that they try this, and if someone else stumbles along this question looking to add support for an unsupported language.

First off, if the language, &/or file-type you are adding is completely unique then this solution will not work, furthermore; if you are able to gain support, it may not be full suport, or it may have other problems. Let me give you an example, as to help you fully understand what it is I am trying to tell you.

Example:


To start, let me show you the syntax for the solution. This is a setting used in V.S. Code's settings.json file — it can be placed in either the user's, or the workspace's implementation of the settings.json file.
   // "./.vscode/settings.json"

   "files.associations": {
       "json": "jsonc"
   },

"Hypothetically, lets say V.S. Code does not support the JSONC file type — 'JSON with Comments'. If the previous statement were true, which it isn't, but if it were, then this solution would work, however, if you have JSONC files already filled with data, then you will likely get errors, as the JSONC file type is used to implement comments in a JSON file, however, V.S. Code's JSON language does not support comments, and the editor will display errors for every comment that there is in a JSONC document, because when you configure your editor this way, V.S. code treats JSONC files as JSON files."



"There are other instances where using this configuration works better. Since we already used JSONC as an example, I'll give you a real world example, where the File-Associations Setting is used to support comments in .json (JSON) files — remember, the above was a hypothetical example, this one is an actual example practiced by developers."
"Adding comments to JSON file is simple, and quick, using the File Association Setting, simple invert the configuration from the code block above, so that the settings configuration looks like the example below. If you type the configuration into your .vscode/settings.json file, you will find that V.S. Code won't complain when you add a comment to a JSON file anymore.
    // "./.vscode/settings.json"  

    "files.associations": {
        "jsonc": "json"
    }, 
Basically, in a nutshell, you may, and you may not, be able to get some support, possibly limited support, or maybe full support, using the File Associations (file.associations) setting. It's easy to try, and it is much faster, and far less work, than to add support to V.S. Code for an entire programming language, or data file-type.

(EDITED: Dec 04th 2021)


I wanted to insert here that I have been taking a C++ class, and I use CLang-format-12 for my formatter. CLang-Format requires that users implement a .clang-format file in the project to customize the configuration of CLang-format (if your familiar with JS, it is the equivalent to an .eslintrc file for ESLint). Sometimes VS-Code auto detects the file as being in YAML format, which is accurate, however, sometimes it detects it as being python??? I don't write python, but something in the syntax seems python'ishy to the "VSCode Auto-detect file format feature", sometimes it detects it as MD, but it isn't in markdown format. It is in YAML format, but it technically is not a YAML document, however, using the file.associations setting I am able to accurately gain support for the file.

FYI, VSCode actually tries its best to add the support on its own, and detect what format a file is in, and what data-format/programming-language grammar, that it has access to, will work for the file your trying to use, but sometimes it needs some help. As I already stated, sometimes a file format will work, but the support might be limited, you will always need to tell VSCode what files types will work when support is limited.



2021-JUNE-23rd @ 22:45 UTC  —  ORIGINAL POST

Solutions for Supporting JSONL in V.S. Code


No one has created support for JSONL files in VSCode. Like you, I could not find an extension that adds language support for JSON Lines or JSONL files. The only way you will get support in VSCode, is if you add the support to VSCode. VSCode offers its users the tools they need to define a language. The tools they offer are the same tools that the MS VSCode development team uses to define language, in other words, the are good tools, and you can do cool stuff with them. This might not be worth considering if it was an entire language, but its not, its just a couple small additions to a preexisting data type. The hardest part will be reading the VSCode docs to learn how to use the Contributes property in the Extension Manifest, which is just a glorified package.json file.

I know that no-one likes to do extra work, especially if its more work than they have too, but I promise you that once you learn the basics of VSCode extension manifest & its contributes properties, plus a little bit about how TextMate works, you can do what you want to do in less than a few hours. Also the payoff is huge, learning how to make VSCode extensions is a decision I was really happy to make, I use the knowledge almost daily. Plus its the only way your gonna get support.



Here are a couple good places to start:


LANGUAGE DEFINITIONS (Official VSCode Website)
SYNTAX HIGHLIGHTING (Official VSCode Website)
CONTRIBUTES OBJECT (Official VSCode Website)
EXTENSION MANIFEST (Official VSCode Website)
TEXTMATE GRAMMARS DOCS (TextMate Official Website)
TEXTMATE TOKENS/SCOPES IN VSCODE (Official VSCode Website)



FINAL NOTE:

The only other option you have would be to email who ever it is that is in charge of JSON Lines, and ask them what editor they suggest using, or ask them if they plan on adding support to VSCode in the future.



JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
  • 4
    Theoretically, JSONlines should be just an extension of JSON but with an additional rule for linebreaks that separate documents. Here YAML might also help as it supports multiple documents and is a superset of JSON. Instead of YAML `---`, in JSONlines `\n` separates documents. But then you would also need to update any parsing rule that allows linebreaks in any other place. - As said above, a few hours might be enough. – E. Körner Jul 14 '21 at 07:31
1

Asa result of this commit: https://github.com/microsoft/vscode/commit/0adddd82816db666c52038e8277312dc55da2756 in the Insider's Build v1.79.0 now is built-in support for the JSON Lines language definition. There is a new jsonl language identifier and the examples below use the .jsonl extension, the JSON Lines lnaguage identifier is automatically chosen.

Here is the default syntax coloring:

JSON Lines default syntax colors

However, you can examine the scopes of these JSON Lines tokens and change the colors yourself through "editor.tokenColorCustomizations" setting like this:

"editor.tokenColorCustomizations": {
    "textMateRules": [

      {
        "scope": "string.quoted.double.json.lines",   // values
        "settings": {
          "foreground": "#7300ff",
          "fontStyle": "bold"
        }
      },
      {
        "scope": "support.type.property-name.json.lines",   // keys/property names
        "settings": {
          "foreground": "#FF0000",
          "fontStyle": "bold"
        }
      }
    ]
},

There are more scopes to investigate. Here is what those token color changes look like:

JSON Lines custom syntax colors

Mark
  • 143,421
  • 24
  • 428
  • 436