Return to Home
Published: Sun Aug 25 2024TR
#VS Code#JSON Schema#Validation#IntelliSense#JSON
Table of Contents

Using JSON Schema in VS Code - Example

VS Code can automatically validate JSON files against a schema and show errors if the data is incorrect.
Follow these steps to use JSON Schema validation in VS Code.


1. Create a JSON Schema File (orderSchema.json)

First, create a schema and save it as orderSchema.json.
This schema will validate order data.

JSON

2. Create a JSON File (order.json)

Now, create a JSON file and save it as order.json.
By adding $schema, VS Code will automatically validate the data against the schema.

JSON

Since "$schema": "./orderSchema.json" is added:
VS Code automatically validates the data against the schema.
If there are errors, VS Code will highlight them immediately.


3. Alternative: Define Schema in VS Code Settings

If you don’t want to add $schema in your JSON files,
you can configure VS Code to apply the schema automatically in settings.json.

Steps:

  1. Open settings.json in VS Code
  • Press Ctrl + Shift + P
  • Search for "Preferences: Open Settings (JSON)" and select it
  1. Add the following setting:
JSON

What does this do?

  • When you open order.json, VS Code automatically validates it using orderSchema.json.
  • If the JSON is invalid, VS Code will display errors immediately.

4. Invalid JSON Example & VS Code Warnings

The following JSON is invalid because:

  • "orderId" has an incorrect format
  • "paymentMethod" is "Bitcoin", which is not allowed
JSON

VS Code will show these errors:

  1. "orderId" does not match the required format (should be 8 characters long).
  2. "paymentMethod" cannot be "Bitcoin" (allowed values: "Credit Card", "PayPal", "Bank Transfer").

SUMMARY

  • Create a JSON schema (orderSchema.json).
  • Prepare a JSON file (order.json).
  • Use $schema or configure settings.json in VS Code to enable validation.
  • VS Code will automatically highlight errors and guide you to fix them.

Now, you can validate JSON files easily in VS Code!

Previous Docker Usage Guide
Next Deploying Calibre-Web on Coolify with Docker Compose
Random GitHub Account Notifications to Gotify with n8n

Search the archive

Posts, gists, projects, repositories, and links in one index.

An unhandled error has occurred. Reload