Reference: Getting started with JSON schema
I have been reading about JSON schema. I understand that
When you’re talking about a data format, you want to have metadata about what keys mean, including the valid inputs for those keys. JSON Schema is a proposed IETF standard how to answer those questions for data.
Alright, so these schemas define what is and what is not permitted in the JSON structure I am building.
My question is, how are these schemas practically used? For example if I am using a JSON file in a C program (or a python script), I can use the json file as it is (of course without any validation). But if I want to validate it, how can I use the json schemas to do that? Are there any recommended libraries for that? (I am interested in C but additional info on python would be welcomed too)
EDIT: I would like to emphasize that the main purpose of this question is to understand how are these schemas practically used?
Are schemas used only for validation? or are there other uses? (I am new to the concept of schemas)
CodePudding user response:
One use is validation. More than pass/fail you get a meaningful error message like e.g. "unexpected value W
for field A.B.C
, allowed values are X, Y, Z
" or "invalid type for field A.B.C
, expected date, found int", "missing field A.B.C" etc.
They can also serve as self documentation.
They are also used for autocomplete. For instance a json setting file for a program like VS Code. When you edit the settings.json
or c_cpp_properties.json
from within VS Code you get autocomplete for that particular json file. That is built in. But you can also define your own schemas with file pattern match and and you can get autocomplete in the editor for your own json files.
CodePudding user response:
The implementations page on the JSON Schema website lists several usecases. https://json-schema.org/implementations.html
- validation
- Code generation
- UI generation
- Sample data generation
JSON Schema is only designed for validation, however other use cases are possible, and we are trying to formalise semantics and additional keywords to make other usecases work interoperably.
Further, if you'd like to hear about real world use cases, there's a series on the official YouTube channel: https://youtube.com/playlist?list=PLHVhS4Tj1YZOrrvl7_a9LaBAtst7BWH8a
There are also some case studies found on the blog: https://json-schema.org/blog