Json stands for JavaScript Object Notation. JSON is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types and is language independent. It is derived from javascript. The official Internet media type for JSON is application/json. JSON filenames use the extension .json.
JSON was created because there was a need for light weight data interchange format. There are other formats also like XML,YAML, HOCON but there was a need for less verbose light weight data interchange format. JSON fulfills all these requirements.
Douglas Crockford originally specified the JSON format in the early 2000s; two competing standards, RFC 8259 and ECMA-404,defined it in 2017. The ECMA standard describes only the allowed syntax, whereas the RFC covers some security and interoperability considerations. The acronym originated at State Software, a company co-founded by Crockford and others in March 2001.
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.
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.
In JSON, they take on these forms:
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
Sample Json format {"employees":[ { "firstName":"John", "lastName":"Doe" }, { "firstName":"Anna", "lastName":"Smith" }, { "firstName":"Peter", "lastName":"Jones" } ]} Sample XML format <?xml version="1.0" encoding="UTF-8" ?> <root> <employees> <firstName>John</firstName> <lastName>Doe</lastName> </employees> <employees> <firstName>Anna</firstName> <lastName>Smith</lastName> </employees> <employees> <firstName>Peter</firstName> <lastName>Jones</lastName> </employees> </root> Sample YAML format --- employees: - firstName: John lastName: Doe - firstName: Anna lastName: Smith - firstName: Peter lastName: Jones
Parsing Json
JSON objects are surrounded by curly braces {}.
JSON objects are written in key/value pairs.
Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).
Keys and values are separated by a colon.
Each key/value pair is separated by a comma.
Parsin
Most of the programming languages has inbuilt support for reading and writing json data. Generally Json data which you would parse comes in two forms
1: As a Json string
2: As a json file
Parsing Json string in different programming language: For example lets take an example JSON string
{
“widget”: {
“debug”: “on”,
“window”: {
“title”: “Sample Konfabulator Widget”,
“name”: “main_window”,
“width”: 500,
“height”: 500
},
“image”: {
“src”: “Images/Sun.png”,
“name”: “sun1”,
“hOffset”: 250,
“vOffset”: 250,
“alignment”: “center”
},
“text”: {
“data”: “Click Here”,
“size”: 36,
“style”: “bold”,
“name”: “text1”,
“hOffset”: 250,
“vOffset”: 100,
“alignment”: “center”,
“onMouseUp”: “sun1.opacity = (sun1.opacity / 100) * 90;”
}
}
}