JSON editing in Visual Studio Code (2024)

JSON is a data format that is common in configuration files like package.json or project.json. We also use it extensively in Visual Studio Code for our configuration files. When opening a file that ends with .json, VS Code provides features to make it simpler to write or modify the file's content.

JSON editing in Visual Studio Code (1)

IntelliSense and validation

For properties and values, both for JSON data with or without a schema, we offer up suggestions as you type with IntelliSense. You can also manually see suggestions with the Trigger Suggestions command (⌃Space (Windows, Linux Ctrl+Space)).

We also perform structural and value verification based on an associated JSON schema giving you red squiggles. To disable validation, use the json.validate.enable setting.

JSON editing in Visual Studio Code (2)

Package and project dependencies

We also offer IntelliSense for specific value sets such as package and project dependencies in package.json, project.json, and bower.json.

Quick navigation

JSON files can get large and we support quick navigation to properties using the Go to Symbol command (⇧⌘O (Windows, Linux Ctrl+Shift+O)).

JSON editing in Visual Studio Code (3)

Hovers

When you hover over properties and values for JSON data with or without schema, we will provide additional context.

JSON editing in Visual Studio Code (4)

Formatting

You can format your JSON document using ⇧⌥F (Windows Shift+Alt+F, Linux Ctrl+Shift+I) or Format Document from the context menu.

Folding

You can fold regions of source code using the folding icons on the gutter between line numbers and line start. Folding regions are available for all object and array elements.

In addition to the default JSON mode following the JSON specification, VS Code also has a JSON with Comments (jsonc) mode. This mode is used for the VS Code configuration files such as settings.json, tasks.json, or launch.json. When in the JSON with Comments mode, you can use single line (//) as well as block comments (/* */) as used in JavaScript. The mode also accepts trailing commas, but they are discouraged and the editor will display a warning.

The current editor mode is indicated in the editor's Status Bar. Select the mode indicator to change the mode and to configure how file extensions are associated to modes. You can also directly modify the files.associations setting to associate file names or file name patterns to jsonc.

JSON schemas and settings

To understand the structure of JSON files, we use JSON schemas. JSON schemas describe the shape of the JSON file, as well as value sets, default values, and descriptions. The JSON support shipped with VS Code supports all draft versions from draft 4 to draft 7, with limited support for drafts 2019-09 and 2020-12.

Servers like JSON Schema Store provide schemas for most of the common JSON-based configuration files. However, schemas can also be defined in a file in the VS Code workspace, as well as the VS Code settings files.

The association of a JSON file to a schema can be done either in the JSON file itself using the $schema attribute, or in the User or Workspace settings (File > Preferences > Settings) under the property json.schemas.

VS Code extensions can also define schemas and schema mapping. That's why VS Code already knows about the schema of some well-known JSON files such as package.json, bower.json, and tsconfig.json.

Mapping in the JSON

In the following example, the JSON file specifies that its contents follow the CoffeeLint schema.

{ "$schema": "https://json.schemastore.org/coffeelint", "line_endings": "unix"}

Note that this syntax is VS Code-specific and not part of the JSON Schema specification. Adding the $schema key changes the JSON itself, which systems consuming the JSON might not expect, for example, schema validation might fail. If this is the case, you can use one of the other mapping methods.

Mapping in the User Settings

The following excerpt from User Settings shows how .babelrc files are mapped to the babelrc schema located on https://json.schemastore.org/babelrc.

"json.schemas": [ { "fileMatch": [ "/.babelrc" ], "url": "https://json.schemastore.org/babelrc" }]

Tip: In addition to defining a schema for .babelrc, also make sure that .babelrc is associated to the JSON language mode. This is also done in the settings using the files.association array setting.

Mapping to a schema in the workspace

To map a schema that is located in the workspace, use a relative path. In this example, a file in the workspace root called myschema.json will be used as the schema for all files ending with .foo.json.

"json.schemas": [ { "fileMatch": [ "**/*.foo.json" ], "url": "./myschema.json" }]

Mapping to a schema defined in settings

To map a schema that is defined in the User or Workspace settings, use the schema property. In this example, a schema is defined that will be used for all files named .myconfig.

"json.schemas": [ { "fileMatch": [ "/.myconfig" ], "schema": { "type": "object", "properties": { "name" : { "type": "string", "description": "The name of the entry" } } } }]

Mapping a schema in an extension

Schemas and schema associations can also be defined by an extension. Check out the jsonValidation contribution point.

File match syntax

The file match syntax supports the '*' wildcard. Also, you can define exclusion patterns, starting with '!'. For an association to match, at least one pattern needs to match and the last matching pattern must not be an exclusion pattern.

 "json.schemas": [ { "fileMatch": [ "/receipts/*.json", "!/receipts/*.excluded.json" ], "url": "./receipts.schema.json" } ]

Define snippets in JSON schemas

JSON schemas describe the shape of the JSON file, as well as value sets and default values, which are used by the JSON language support to provide completion proposals. If you are a schema author and want to provide even more customized completion proposals, you can also specify snippets in the schema.

The following example shows a schema for a key binding settings file defining a snippet:

{ "type": "array", "title": "Keybindings configuration", "items": { "type": "object", "required": ["key"], "defaultSnippets": [ { "label": "New keybinding", "description": "Binds a key to a command for a given state", "body": { "key": "$1", "command": "$2", "when": "$3" } } ], "properties": { "key": { "type": "string" } ... } }}

This is an example in a JSON schema:

JSON editing in Visual Studio Code (5)

Use the property defaultSnippets to specify any number of snippets for the given JSON object.

  • label and description will be shown in the completion selection dialog. If no label is provided, a stringified object representation of the snippet will be shown as label instead.
  • body is the JSON object that is stringified and inserted when the completion is selected by the user. Snippet syntax can be used inside strings literals to define tabstops, placeholders, and variables. If a string starts with ^, the string content will be inserted as-is, not stringified. You can use this to specify snippets for numbers and booleans.

Note that defaultSnippets is not part of the JSON schema specification but a VS Code-specific schema extension.

Use rich formatting in hovers

VS Code will use the standard description field from the JSON Schema specification in order to provide information about properties on hover and during autocomplete.

If you want your descriptions to support formatting like links, you can opt in by using Markdown in your formatting with the markdownDescription property.

{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "name": { "type": "string", "description": "The name of the entry", "markdownDescription": "The name of the entry. [See the documentation](https://example.com)" } }}

Note that markdownDescription is not part of the JSON schema specification but a VS Code-specific schema extension.

Offline mode

json.schemaDownload.enable controls whether the JSON extension fetches JSON schemas from http and https.

A warning triangle will show in the status bar when the current editor would like to use schemas that cannot be downloaded.

02/28/2024

JSON editing in Visual Studio Code (2024)

FAQs

How do I edit JSON files in VS Code? ›

You can customize these settings by editing the settings. json file directly or using the settings UI editor. To edit settings. json , utilize the Command Palette Cmd + Shift + P for macOS or Ctrl + Shift + P for Windows and then type “Preferences: Open Settings (JSON)”.

How to edit JSON code? ›

Especially JSON files used as translation resources. You can use any text/code editor such as Visual Studio Code, Notepad, Notepad++, Sublime Text and others to open and edit JSON files. Alternatively, you can use an online editor to edit your JSON files.

How do I open JSON settings in Visual Studio code? ›

You can open the settings.json file with the Preferences: Open User Settings (JSON) command in the Command Palette (Ctrl+Shift+P).

How to make JSON readable in VS Code? ›

Formatting JSON in Visual Studio Code
  1. Just right-click and select “Format Document“, or press Ctrl+Shift+I . ...
  2. The resulting formatted JSON. ...
  3. If it's not a file, it's not so simple any more… ...
  4. Press Ctrl+Shift+P , then select “Change Language Mode“. ...
  5. Select “JSON” from the list of languages. ...
  6. “Format Document” is now possible.
Dec 2, 2022

How to edit a value in a JSON file? ›

You can:
  1. Use fs. promises. ...
  2. Use JSON. parse to parse the contents into a JavaScript object,
  3. Modify the values of the object in any way you need to.
  4. Use JSON. stringify to create a JSON string from the object.
  5. Use fs.promises.writeFile to write the new contents into the file you've.
Feb 8, 2023

How to edit JSON file using Notepad? ›

With the plugin installed, open the JSON file you want to format in Notepad++. Go to the Plugins menu and select the JSON Viewer option. This will open a new window that displays the JSON code with proper indentation and color-coded syntax highlighting to make it easier to read and edit.

Can Notepad++ edit a JSON file? ›

In conclusion, Notepad++ is an incredibly powerful text editor that offers a range of tools and features for efficiently formatting your JSON code.

What is the command line tool to edit JSON files? ›

Repository files navigation

JJ is a command line utility that provides a fast and simple way to retrieve or update values from JSON documents. It's powered by GJSON and SJSON under the hood.

How to update existing JSON file? ›

Updating or Merging the Existing JSON File
  1. Create a new. synchronization. task.
  2. In. Definition. tab, select. Update. as the task operation.
  3. Follow the same procedure of creating a JSON Target file using Insert task operation to update the existing JSON file.

How to add json in Visual Studio? ›

In Visual Studio, you can copy text from JSON or XML files and then paste the text as classes in your C# or Visual Basic code. To do so, select Edit > Paste Special and choose either Paste JSON As Classes or Paste XML As Classes.

How to update VS Code? ›

You can also manually check for updates by running Help > Check for Updates on Linux and Windows or running Code > Check for Updates on macOS. Note: You can disable auto-update if you prefer to update VS Code on your own schedule.

How to install json library in Visual Studio Code? ›

Install Json.Net using the Visual Studio Solution Explorer. #
  1. Right click the References node in your Project and click Manage Nuget Packages...
  2. In the Nuget Package Manager Dialog box, make sure Online is selected in the left pane. Type Json.Net in the search box in the top right. ...
  3. Click the Install button.

How to format JSON automatically in VS Code? ›

Background
  1. Cmd + K and then M to complete the command.
  2. Type JSON.
  3. Hit Enter.
  4. Option + Shift + F to format the content.
Feb 11, 2023

How to make JSON file readable? ›

If you need to convert a file containing Json text to a readable format, you need to convert that to an Object and implement toString() method(assuming converting to Java object) to print or write to another file in a much readabe format. You can use any Json API for this, for example Jackson JSON API.

How to properly format a JSON file? ›

Valid JSON data can be in two different formats: A collection of key-value pairs enclosed by a pair of curly braces {...} . You saw this as an example above. A collection of an ordered list of key-value pairs separated by comma (,) and enclosed by a pair of square brackets [...] .

How to add JSON file in Visual Studio Code? ›

To create a launch.json file, select create a launch.json file in the Run start view. If you go back to the File Explorer view (Ctrl+Shift+E), you'll see that VS Code has created a .vscode folder and added the launch.json file to your workspace.

How to convert JSON to object in VS Code? ›

How To Use
  1. Select a valid JSON object in your editor (if nothing is selected then the whole file is checked)
  2. Choose Convert JSON to JS Object in the command palette ( Cmt/Ctrl+Shift+P )
Mar 15, 2017

How to edit a value in JSON file using Python? ›

Modify JSON Fields Using update() Method

In this example, below code parses a sample JSON data string representing a person's information, changes the 'name' field from “Eva” to “Sophia,” and then converts the modified data back to JSON format. The resulting JSON, with the updated name, is printed to the console.

Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 6001

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.