JSON Schema

JSON Schema

What is JSON?
JSON Stands for JavaScript Object Notation. This is a structure that can be used to store data as in key value pair separated by : . JSON can be used to transfer data to and fro any network. It is a simple plain text, it can be consumed by any programming language.

For example the address information can be stored in json format

{
"street":"Station Road"
"city" : "Hyderabad",
"state":"Telengana",
"pincode":"500016"
}

The value may be simple text or it may be an array, or json object as well.

What is JSON Schema?
To validate the values against a given json we can have schema. The schema is a template that can be used to check whether a given JSON is valid or invalid.

For example the above address json the key “city” is supposed to have some values in string/text if the “city” has some arbitary number “3445545” or a decimal values “34.34344” then it can lead to wrong information. The application consuming this JSON will have wrong data. Thus JSON schema help us to define some rules that can be used to validate a JSON.
How to define a JSON Schema?
Consider the following schema for the json structure person

{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}

The key “title” is the title of the shema.
The key “type” defines the type of json. PersonJson It may be object, array etc.
The key “properties” has the actual definition of the PersonJson.
The firstName of the Person should be of type string
The lastName of the PersonJson should be type string.
The age of the PersonJson should be of type integer. The minimum value is 0
The key “required” in the schema tells which keys are mandatory and should be part of the PersonJson.

The Json schema does not restrict us to define native type but also we can have custom definition.

How to validate the Json against the Schema?
There are various libraries for different programming language to validate the schema against the Json.
Programming Language and popular schema library in json

{
"C": "WJElement (LGPLv3).",
"Java": "json-schema-validator (LGPLv3).",
"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"
}

More information can be obtained at http://json-schema.org/

Somen                                                                                                                    Sources: internet

JSON – Brief Introduction

JSON stands for JavaScript Object Notation.it is  lightweight .It gives us text-based open standard designed for human-readable  collection of data that we can access in a really logical manner.

It is used primarily to transmit data between a server and web application, as an alternative to XML.

Although originally derived from the JavaScript scripting language, JSON is a language-independent data format, and code for parsing and generating JSON data is readily available in a large variety of programming languages.

 

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

example:

{“children”:[{“name”:”1″,”size”:0},{“name”:”2″,”size”:0},{“name”:”3″,”size”:0.4},{“name”:”4″,”size”:0.5882},{“name”:”5″,”size”:19.4}]}

Steps to create json in java.

Jar files required for json

1.commons-beanutils-1.8.1

2.commons-collections-3.2.1

3.commons-lang-2.5

4.commons-logging-1.1.1

5.ezmorph-1.0.6

6.java-json

7.json-rpc-1.0

Put all the above jar files in WEB-INF/lib folder

create a class to create convert data into json

package com.helical.efw.json;

import java.util.List;

import java.util.Map;

import net.sf.json.JSONObject;

public class ConvertJson {

 

public void CommonJson(Map<String, List<Buble>> commonList)

{

String jsonString;

//Suppose in commonList data comming in this format

//{children=[{name=1, size=0.0000}, {name=2, size=0.0000},

//{name=3, size=0.4000}, {name=4, size=0.5882}, {name=5,

//size=19.4000}]}

JSONObject json = new JSONObject();

json.accumulateAll(commonList);

jsonString=json.toString();

System.out.println(json.toString());

}

}

 

 

Public class JsonProducer{

public static void main(String[] args) throws IOException {

ConvertJson cjson=new ConvertJson();

cjson.CommonJson();

}

JSONObject is a class in which there are so many methods available.

like:

JSONObject.accumulateAll(Map) :-It Accumulate values under a key

JSONObject.getJSONObject(java.lang.String key ):-we can get  get the JSONObject value associated with a key.