JSON – Schema ”; Previous Next JSON Schema is a specification for JSON based format for defining the structure of JSON data. It was written under IETF draft which expired in 2011. JSON Schema − Describes your existing data format. Clear, human- and machine-readable documentation. Complete structural validation, useful for automated testing. Complete structural validation, validating client-submitted data. JSON Schema Validation Libraries There are several validators currently available for different programming languages. Currently the most complete and compliant JSON Schema validator available is JSV. Languages Libraries C WJElement (LGPLv3) Java json-schema-validator (LGPLv3) .NET Json.NET (MIT) ActionScript 3 Frigga (MIT) Haskell aeson-schema (MIT) Python Jsonschema Ruby autoparse (ASL 2.0); ruby-jsonschema (MIT) PHP php-json-schema (MIT). json-schema (Berkeley) JavaScript Orderly (BSD); JSV; json-schema; Matic (MIT); Dojo; Persevere (modified BSD or AFL 2.0); schema.js. JSON Schema Example Given below is a basic JSON schema, which covers a classical product catalog description − { “$schema”: “http://json-schema.org/draft-04/schema#”, “title”: “Product”, “description”: “A product from Acme”s catalog”, “type”: “object”, “properties”: { “id”: { “description”: “The unique identifier for a product”, “type”: “integer” }, “name”: { “description”: “Name of the product”, “type”: “string” }, “price”: { “type”: “number”, “minimum”: 0, “exclusiveMinimum”: true } }, “required”: [“id”, “name”, “price”] } Let”s the check various important keywords that can be used in this schema − Sr.No. Keyword & Description 1 $schema The $schema keyword states that this schema is written according to the draft v4 specification. 2 title You will use this to give a title to your schema. 3 description A little description of the schema. 4 type The type keyword defines the first constraint on our JSON data: it has to be a JSON Object. 5 properties Defines various keys and their value types, minimum and maximum values to be used in JSON file. 6 required This keeps a list of required properties. 7 minimum This is the constraint to be put on the value and represents minimum acceptable value. 8 exclusiveMinimum If “exclusiveMinimum” is present and has boolean value true, the instance is valid if it is strictly greater than the value of “minimum”. 9 maximum This is the constraint to be put on the value and represents maximum acceptable value. 10 exclusiveMaximum If “exclusiveMaximum” is present and has boolean value true, the instance is valid if it is strictly lower than the value of “maximum”. 11 multipleOf A numeric instance is valid against “multipleOf” if the result of the division of the instance by this keyword”s value is an integer. 12 maxLength The length of a string instance is defined as the maximum number of its characters. 13 minLength The length of a string instance is defined as the minimum number of its characters. 14 pattern A string instance is considered valid if the regular expression matches the instance successfully. You can check a http://json-schema.org for the complete list of keywords that can be used in defining a JSON schema. The above schema can be used to test the validity of the following JSON code − [ { “id”: 2, “name”: “An ice sculpture”, “price”: 12.50, }, { “id”: 3, “name”: “A blue mouse”, “price”: 25.50, } ] Print Page Previous Next Advertisements ”;
Category: json
JSON – Home
JSON Tutorial PDF Version Quick Guide Resources Job Search Discussion JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. The JSON format was originally specified by Douglas Crockford, and is described in RFC 4627. The official Internet media type for JSON is application/json. The JSON filename extension is .json. This tutorial will help you understand JSON and its use within various programming languages such as PHP, PERL, Python, Ruby, Java, etc. Audience This tutorial has been designed to help beginners understand the basic functionality of JavaScript Object Notation (JSON) to develop the data interchange format. After completing this tutorial, you will have a good understanding of JSON and how to use it with JavaScript, Ajax, Perl, etc. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of the web application’s work over HTTP and we assume that you have a basic knowledge of JavaScript. Frequently Asked Questions about JSON In this section, we will answer some Frequently Asked Questions(FAQ) about JSON. What is the full-form JSON? JSON is an abbreviation that stands for JavaScript Object Notation. What is a JSON used for? The most common use of JSON is to transmit data between server and client. It can also be used as a temporary storage. Who developed JSON? The JSON was developed by Douglas Crockford. What is an extension of a JSON object file? The file extension for JSON object files is “.json”. How to Create JSON File? JSON file can be created using any text editor by saving the file with a “.json” extension. How to write JSON code? Writing JSON code is somewhat similar to the syntax of JavaScript object notation. The data field consists of name/value pairs separated by commas. It uses curly brackets to hold the objects and square brackets to hold the arrays. How to Open JSON File? Similar to the other plain text files, we can open JSON files in any text editor or browser. What are the different browsers that support JSON? All modern browsers including Firefox, Safari, Edge, Opera and Chrome support JSON. What are some of the applications of JSON? Following are the applications of JSON − JSON can store temporary data. We can easily integrate JSON data with various programming languages such as Python, Ruby, Java and so on. With the help of a network, JSON can transfer data across multiple devices. What is JSON Schema, and how does it work? JSON Schema is a specification used for describing and validating JSON data. It ensures the integrity of JSON data and its consistency across different applications. Print Page Previous Next Advertisements ”;
JSON – Syntax
JSON – Syntax ”; Previous Next Let”s have a quick look at the basic syntax of JSON. JSON syntax is basically considered as a subset of JavaScript syntax; it includes the following − Data is represented in name/value pairs. Curly braces hold objects and each name is followed by ”:”(colon), the name/value pairs are separated by , (comma). Square brackets hold arrays and values are separated by ,(comma). Below is a simple example − { “book”: [ { “id”: “01”, “language”: “Java”, “edition”: “third”, “author”: “Herbert Schildt” }, { “id”: “07”, “language”: “C++”, “edition”: “second”, “author”: “E.Balagurusamy” } ] } JSON supports the following two data structures − Collection of name/value pairs − This Data Structure is supported by different programming languages. Ordered list of values − It includes array, list, vector or sequence etc. Print Page Previous Next Advertisements ”;
JSON – DataTypes
JSON – DataTypes ”; Previous Next JSON format supports the following data types − Sr.No. Type & Description 1 Number double- precision floating-point format in JavaScript 2 String double-quoted Unicode with backslash escaping 3 Boolean true or false 4 Array an ordered sequence of values 5 Value it can be a string, a number, true or false, null etc 6 Object an unordered collection of key:value pairs 7 Whitespace can be used between any pair of tokens 8 null empty Number It is a double precision floating-point format in JavaScript and it depends on implementation. Octal and hexadecimal formats are not used. No NaN or Infinity is used in Number. The following table shows the number types − Sr.No. Type & Description 1 Integer Digits 1-9, 0 and positive or negative 2 Fraction Fractions like .3, .9 3 Exponent Exponent like e, e+, e-, E, E+, E- Syntax var json-object-name = { string : number_value, …….} Example Example showing Number Datatype, value should not be quoted − var obj = {marks: 97} String It is a sequence of zero or more double quoted Unicode characters with backslash escaping. Character is a single character string i.e. a string with length 1. The table shows various special characters that you can use in strings of a JSON document − Sr.No. Type & Description 1 “ double quotation 2 backslash 3 / forward slash 4 b backspace 5 f form feed 6 n new line 7 r carriage return 8 t horizontal tab 9 u four hexadecimal digits Syntax var json-object-name = { string : “string value”, …….} Example Example showing String Datatype − var obj = {name: ”Amit”} Boolean It includes true or false values. Syntax var json-object-name = { string : true/false, …….} Example var obj = {name: ”Amit”, marks: 97, distinction: true} Array It is an ordered collection of values. These are enclosed in square brackets which means that array begins with .[. and ends with .].. The values are separated by , (comma). Array indexing can be started at 0 or 1. Arrays should be used when the key names are sequential integers. Syntax [ value, …….] Example Example showing array containing multiple objects − { “books”: [ { “language”:”Java” , “edition”:”second” }, { “language”:”C++” , “lastName”:”fifth” }, { “language”:”C” , “lastName”:”third” } ] } Object It is an unordered set of name/value pairs. Objects are enclosed in curly braces that is, it starts with ”{” and ends with ”}”. Each name is followed by ”:”(colon) and the key/value pairs are separated by , (comma). The keys must be strings and should be different from each other. Objects should be used when the key names are arbitrary strings. Syntax { string : value, …….} Example Example showing Object − { “id”: “011A”, “language”: “JAVA”, “price”: 500, } Whitespace It can be inserted between any pair of tokens. It can be added to make a code more readable. Example shows declaration with and without whitespace − Syntax {string:” “,….} Example var obj1 = {“name”: “Sachin Tendulkar”} var obj2 = {“name”: “SauravGanguly”} null It means empty type. Syntax null Example var i = null; if(i == 1) { document.write(“<h1>value is 1</h1>”); } else { document.write(“<h1>value is null</h1>”); } JSON Value It includes − number (integer or floating point) string boolean array object null Syntax String | Number | Object | Array | TRUE | FALSE | NULL Example var i = 1; var j = “sachin”; var k = null; Print Page Previous Next Advertisements ”;