Use code completion for your custom JSON format in just 1 minute

TL;DR: How you can to enable the code completion for your Visual Studio Code IDE

JSON Schema rules!

Motivation

The code completion is one of the most worth and important features of an IDE. Since the first version of Visual Basic or Visual C++, the presence of “IntelliSense” feature improved the productivity of thousands of programmers.

Normally, to create a custom code-completion bundle for your favorite programming or scripting language is a non trivial task.

But in the case of JSON formats, that task is not so hard.

The JSON Schema

As you probably knows the JavaScript Object Notation (JSON) is a structureless format.

You can define whatever properties, arrays, etc.

The JSON schema enters to add schema to specify a custom JSON format for a specific payload.

We’ll not go into the details, just say that a JSON schema is another JSON file where we describe the properties and types about our objects.

In the following example, we define the types and required element of a JSON object to specify a calendar data type.

You can check how easy it is to define a data type using this scheme.

Here it’s important the meta-properties, named with the prefix “$”. With “$ref” we can reference others JSON schema in order to build complex types using schema recursivity.

How a JSON schema is referenced?

In order for your IDE to know the schema that your JSON object must comply with, you just have to include the meta-property $schema with the URI where said schema resides.

Continuing with the previous example, if I’m specifying a new calendar JSON, just I need to add a line like to:

{
$schema: './schemas/calendar.json'
...
}

Easy, right? (In this case, our schema is stored locally, but it can be any URL)

If we are coding it with Visual Studio Code, the code completion starts working as soon as you specify the schema with $schema

Just say the magic word “$schema” and VS Code does the rest

Auto Generated schema

Despite the JSON schema rules are simple, you can auto-generate the schema from JSON files of examples.

The following free and online tool lets you to do it very easily: copy and paste the body of the example JSON and click on “Generate”. Done!

Conclusion

Write JSON files without code completion not only will it slow you down, but your productivity will not be as optimal as possible.

As you have seen, you are only 1 minute away from saving hours in your work. Seize it!

Please, if you liked it, give it a round of applause. And if you want to know more about DevOps, Kubernetes, Docker, etc … follow me :)

Thanks so much! =)

CTO @ Digitalilusion.com & DigitalSecured.net Beyond-Full-stack developer #go #python #kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store