{"id":275,"date":"2018-05-01T11:37:18","date_gmt":"2018-05-01T11:37:18","guid":{"rendered":"https:\/\/restfulapi.net\/?p=275"},"modified":"2021-09-27T22:26:25","modified_gmt":"2021-09-27T16:56:25","slug":"json-schema","status":"publish","type":"post","link":"https:\/\/restfulapi.net\/json-schema\/","title":{"rendered":"JSON Schema"},"content":{"rendered":"\n
It\u2019s often necessary for applications to validate JSON objects, to ensure that required properties are present and that additional validation constraints (such as a price never being less than one dollar) are met. These validations are typically performed in the context of JSON Schema<\/em>.<\/p>\n\n\n\n When we validate a JSON document without its Schema, we are validating only the syntax of the document. Syntactic validation guarantees only that the document is well-formed.<\/p>\n\n\n\n The tools such as JSONLint<\/a>, and the JSON parsers perform only Syntactic validations.<\/p>\n\n\n\n Semantic validation is more than syntactic validation. It performs the syntax checks as well as the data checks.<\/p>\n\n\n\n Semantic validation helps in ensuring that the client is sending only allowed fields in JSON documents and not any invalid name-value pairs.<\/p>\n\n\n\n It can also help in checking the format of a phone number, a date\/time, a postal code, an email address, or a credit card number.<\/p>\n\n\n\n JSON Schema is a grammar language for defining the structure, content, and (to some extent) semantics of JSON objects. It lets you specify metadata (data about data) about what an object\u2019s properties mean and what values are valid for those properties.<\/p>\n\n\n\n The result of applying the grammar language to a JSON document is the schema (a blueprint) describing the set of JSON objects that are valid according to the schema.<\/p>\n\n\n\n Let’s consider the following JSN schema :<\/p>\n\n\n\n and JSON document is :<\/p>\n\n\n\n The JSON Schema website provides links to various validator implementations for different programming languages (see http:\/\/json-schema.org\/implementations.html ). We can download implementation and integrate it into our application, subject to licensing requirements.<\/p>\n\n\n\n We have used the online validator at JSONSchemaLint<\/a> in the given example.<\/p>\n\n\n\n1. Syntactic vs Semantic Validation<\/h2>\n\n\n\n
2. What Is JSON Schema?<\/h2>\n\n\n\n
3. JSON Schema Validation Example<\/h2>\n\n\n\n
{\r\n\t\"$schema\": \"http:\/\/json-schema.org\/draft-04\/schema#\",\r\n\t\"title\": \"Person\",\r\n\t\"description\": \"A person\",\r\n\t\"type\": \"object\",\r\n\t\"properties\":\r\n\t{\r\n\t\t\"name\":\r\n\t\t{\r\n\t\t\t\"description\": \"A person's name\",\r\n\t\t\t\"type\": \"string\"\r\n\t\t},\r\n\t\t\"age\":\r\n\t\t{\r\n\t\t\t\"description\": \"A person's age\",\r\n\t\t\t\"type\": \"number\",\r\n\t\t\t\"minimum\": 18,\r\n\t\t\t\"maximum\": 64\r\n\t\t}\r\n\t},\r\n\t\"required\": [\"name\", \"age\"]\r\n}<\/code><\/pre>\n\n\n\n
{\r\n\t\"name\": \"John Doe\",\r\n\t\"age\": 35\r\n}<\/code><\/pre>\n\n\n\n
3.1. Valid JSON document example<\/h4>\n\n\n\n