Professional Documents
Culture Documents
1
2 2 DATA TYPES, SYNTAX AND EXAMPLE
text, realizing real-time rich Web applications using only that each key is unique within an object. Objects
the standard HTTP, HTML and JavaScript capabilities of are delimited with curly brackets and use commas
Netscape 4.0.5+ and IE 5+. Crockford then found that to separate each pair, while within each pair the
JavaScript could be used as an object-based messaging colon ':' character separates the key or name from
format for such a system. The system was sold to Sun Mi- its value.
crosystems, Amazon.com and EDS. The JSON.org Web
site was launched in 2002. In December 2005, Yahoo! null: An empty value, using the word null
began oering some of its Web services in JSON.[9]
JSON was originally intended to be a subset of the Limited whitespace is allowed and ignored around or be-
JavaScript scripting language (specically, Standard tween syntactic elements (values and punctuation, but not
ECMA262 3rd EditionDecember 1999[10] ) and is within a string value). Only four specic characters are
commonly used with Javascript, but it is a language- considered whitespace for this purpose: space, horizon-
independent data format. Code for parsing and generat- tal tab, line feed, and carriage return. In particular, the
ing JSON data is readily available in many programming byte order mark must not be generated by a conforming
languages. JSONs Web site lists JSON libraries by lan- implementation (though it may be accepted when parsing
guage. JSON). JSON does not provide any syntax for comments.
Though JSON was originally advertised and believed to Early versions of JSON (such as specied by RFC 4627)
be a strict subset of JavaScript and ECMAScript,[11] it in- required that a valid JSON document must consist of
advertently allows some unescaped characters in strings only an object or an array type, which could contain other
that are illegal in JavaScript and ECMAScript strings. types within them.
See Data portability issues below.
JSON itself became an ECMA international standard in 2.1 Example
2013 as the ECMA-404 standard.[12] In the same year
RFC 7158 used ECMA-404 as reference. In 2014 RFC
The following example shows a possible JSON represen-
7159 became the main reference for JSONs internet uses
tation describing a person.
(ex. MIME application/json), and obsoletes RFC 4627
and RFC 7158 (but preserving ECMA-262 and ECMA- { rstName": John, lastName": Smith, isAlive":
404 as main references). true, age": 25, address": { streetAddress": 21 2nd
Street, city": New York, state": NY, postal-
Code": 10021-3100 }, phoneNumbers": [ { type":
2 Data types, syntax and example home, number": 212 555-1234 }, { type": of-
ce, number": 646 555-4567 }, { type": mobile,
number": 123 456-7890 } ], children": [], spouse":
JSONs basic data types are: null }
JSON parsers. For example, to include the Emoji charac- with this type of conversion.
ter U+1F602 FACE WITH TEARS OF JOY in JSON:
{ face": " " } // or { face": "\uD83D\uDE02 }
3 Schema and metadata
Numbers in JSON are agnostic with regard to their repre-
sentation within programming languages. No dierenti- 3.1 JSON Schema
ation is made between an integer and oating-point value:
some implementations may treat 42, 42.0, and 4.2E+1 as JSON Schema[18] species a JSON-based format to de-
the same number while others may not. Furthermore, no ne the structure of JSON data for validation, documen-
requirements are made regarding implementation issues tation, and interaction control. It provides a contract for
such as overow, underow, loss of precision, or round- the JSON data required by a given application, and how
ing. Additionally, JSON says nothing about the treat- that data can be modied.
ment of signed zeros: whether 0.0 is distinct from 0.0.
JSON Schema is based on the concepts from XML
Most implementations that use the IEEE 754 oating-
Schema (XSD), but is JSON-based. As in XSD, the same
point standard, including JavaScript, preserve signed ze-
serialization/deserialization tools can be used both for the
ros; but not all JSON implementations may do so.
schema and data; and is self-describing. It is described in
an Internet Draft currently in its 5th version, which was
2.3 Using JSON in JavaScript released on October 13, 2016[19] (version 4 had expired
on August 4, 2013,[20] leading to a lapse of more than 3
Since JSON was derived from JavaScript and its syntax years). There are several validators [21]
available for dier-
is (mostly) a subset of the language, it is possible to use ent programming languages, each with varying levels
the JavaScript eval() function to parse JSON data. Due of conformance.
to the issue with parsing of Unicode line terminators dis- There is no standard le extension, but some have sug-
cussed in the preceding section, the eval function needs gested .schema.json.[22]
to perform a string replacement.[14]
Example JSON Schema (draft 4):
var p = eval('(' + json_string.replace(/\u2028/g,'\\u2028').replace(/\u2029/g,
{ "$schema": "http://json-schema.org/schema#", title":
'\\u2029') + ')');
Product, type": object, required": ["id, name,
price"], properties": { id": { type": number,
This is unsafe if the string is untrusted. Instead, a JSON description": Product identier }, name": { type":
parser library or JavaScripts native JSON support should string, description": Name of the product }, price":
be used for reading and writing JSON. { type": number, minimum": 0 }, tags": { type":
var p = JSON.parse(json_string); array, items": { type": string } }, stock": {
type": object, properties": { warehouse": { type":
number }, retail": { type": number } } } } }
A correctly implemented JSON parser only accepts valid
JSON, preventing potentially malicious code from being
inadvertently executed. The JSON Schema above can be used to test the validity
of the JSON code below:
Since 2010, web browsers such as Firefox and Internet
Explorer have included support for parsing JSON. As na- { id": 1, name": Foo, price": 123, tags": [ Bar,
tive browser support is more ecient and secure than Eek ], stock": { warehouse": 300, retail": 20 } }
eval(), native JSON support is included in Edition 5 of
the ECMAScript standard.[15]
4 MIME type
2.4 Unsupported native data types
JavaScript syntax denes several native data types that The ocial MIME type for JSON text is
are not included in the JSON standard:[13] Map, Set, application/json,[23] and most modern implemen-
Date, Error, Regular Expression, Function, Promise, and tations have adopted this.
undened.[note 1] These JavaScript data types must be rep- The (unocial) MIME type text/json or the content-
resented by some other data format, with the programs type text/javascript also get legacy support by many
on both ends agreeing on how to convert between the service providers, browsers, servers, web applications,
types. As of 2011, there are some de facto standards, libraries, frameworks, and APIs. Notable exam-
e.g., converting from Date to String, but none universally ples include the Google Search API,[24] Yahoo!,[24][25]
recognized.[16][17] Other languages may have a dierent Flickr,[24] Facebook API,[26] Lift framework,[27] Dojo
set of native types that must be serialized carefully to deal Toolkit 0.4,[28] etc.
4 6 SECURITY CONSIDERATIONS
JSON is promoted as a low-overhead alternative to XML The JSON code above is also entirely valid YAML.
as both of these formats have widespread support for cre- YAML also oers an alternative syntax intended to be
ation, reading, and decoding in the real-world situations more human-accessible by replacing nested delimiters
where they are commonly used.[50] Apart from XML, like {}, [], and " marks with o-side indentation.[51]
examples could include OGDL, YAML and CSV. Also, --- rstName: John lastName: Smith age: 25 address:
Google Protocol Buers can ll this role, although it is streetAddress: 21 2nd Street city: New York state: NY
not a data interchange language. postalCode: '10021' phoneNumber: - type: home num-
6 10 NOTES
The properties can also be serialized using attributes in- Jayrockan open source implementation of
stead of tags: JSON for the .NET Framework.
Ember data server implementations for PHP,
<person rstName="John lastName="Smith
Node.js, Ruby, Python, Go, .NET and Java.
age="25"> <address streetAddress="21 2nd Street
city="New York state="NY postalCode="10021 Other
/> <phoneNumber type="home number="212 555-
1234"/> <phoneNumber type="fax number="646 Comparison of data serialization formats
555-4567"/> <gender type="male"/> </person> Jq (programming language)
v}); [17] Tales from the Evil Empire - Dates and JSON. asp.net.
Converting this JSON back into JavaScript is not as Retrieved 13 September 2015.
straightforward. While JSON.parse can take an optional
reviver argument that is, essentially, the inverse of a re- [18] JSON Schema and Hyper-Schema. json-schema.org.
placer, it can't be used in this situation. If that function Retrieved 13 September 2015.
returns undened, the JSON.parse logic interprets this to
[19] draft-wright-json-schema-00 - JSON Schema: A Me-
mean to not dene a property rather than dene one with
dia Type for Describing JSON Documents. json-
a undened value. Instead one has to explicitly post pro-
schema.org/. Retrieved 17 February 2017.
cess the result from JSON.parse replacing each UNDE-
FINED with undened. [20] draft-zyp-json-schema-04 - JSON Schema: core de-
nitions and terminology. ietf.org. Retrieved 17 March
2016.
11 References [21] JSON Schema Software. json-schema.org. Retrieved
13 September 2015.
[1] Doug Crockford Google Tech Talks: JavaScript: The
Good Parts"". 7 February 2009. [22] http://stackoverflow.com/a/10507586/287948
[2] A Modern Reintroduction To AJAX. Retrieved 12 [23] Media Types. iana.org. Retrieved 13 September 2015.
April 2017.
[24] Handle application/json & text/json by benschwarz Pull
[3] Bray, Tim. JSON Redux AKA RFC7159. Ongoing. Request #2 mislav/faraday-stack. GitHub. Retrieved 13
Retrieved 16 March 2014. September 2015.
[4] Bray, Tim (ed.), The I-JSON Message Format, Internet En- [25] Yahoo!, JavaScript, and JSON. ProgrammableWeb.
gineering Task Force (IETF), RFC 7493 Retrieved 13 September 2015.
[5] Douglas Crockford The JSON Saga. YouTube. 28 [26] Make JSON requests allow text/javascript content
August 2011. Retrieved 23 September 2016. by jakeboxer Pull Request #148 AFNetwork-
ing/AFNetworking. GitHub. Retrieved 13 September
[6] Chip Morningstar Biography. n.d. 2015.
[7] State Software Breaks Through Web App Development [27] lift/Req.scala at master lift/lift GitHub. GitHub. Re-
Barrier With State Application Framework: Software trieved 13 September 2015.
Lets Developers Create Truly Interactive Applications;
Reduces Costs, Development Time and Improves User [28] BrowserIO.js in legacy/branches/0.4/src/io Dojo
Experience. PR Newswire. February 12, 2002. Toolkit. dojotoolkit.org. Retrieved 13 September 2015.
[8] Apache and the JSON license on LWN.net by Jake Edge [29] JSON: The JavaScript subset that isn't. Magnus Holm.
(November 30, 2016) Retrieved 16 May 2011.
[9] Yahoo!. Using JSON with Yahoo! Web services. [30] Douglas Crockford (July 2006). IANA Considerations.
Archived from the original on October 11, 2007. Re- The application/json Media Type for JavaScript Object No-
trieved July 3, 2009. tation (JSON). IETF. sec. 6. RFC 4627. https://tools.
ietf.org/html/rfc4627#section-6. Retrieved October 21,
[10] Crockford, Douglas (May 28, 2009). Introducing 2009.
JSON. json.org. Retrieved July 3, 2009.
[31] Stefano Di Paola. Minded Security Blog: Ye Olde
[11] Douglas Crockford (2016-07-10). JSON in JavaScript. Crockford JSON regexp is Bypassable. mindedsecu-
Archived from the original on 2016-07-10. Retrieved rity.com. Retrieved 13 September 2015.
2016-08-13.
[32] Crockford, Douglas (December 6, 2006). JSON: The
[12] The JSON Data Interchange Format (PDF). ECMA Fat-Free Alternative to XML. Retrieved July 3, 2009.
International. October 2013. Retrieved 23 September
2016. [33] ECMAScript Fifth Edition (PDF). Retrieved March 18,
2011.
[13] JSON Web Token (JWT)". IETF. May 2015. Retrieved
23 September 2016. [34] Using Native JSON. June 30, 2009. Retrieved July 3,
2009.
[14] Holm, Magnus (15 May 2011). JSON: The JavaScript
subset that isn't. The timeless repository. Retrieved 23 [35] Barsan, Corneliu (September 10, 2008). Native JSON in
September 2016. IE8. Retrieved July 3, 2009.
[15] Standard ECMA-262. ecma-international.org. Re- [36] Web specications supported in Opera Presto 2.5.
trieved 13 September 2015. March 10, 2010. Retrieved March 29, 2010.
[16] jquery - Format a Microsoft JSON date? - Stack Over- [37] Hunt, Oliver (June 22, 2009). Implement ES 3.1 JSON
ow. stackoverow.com. Retrieved 13 September 2015. object. Retrieved July 3, 2009.
8 12 EXTERNAL LINKS
[45] Zyp, Kris (September 16, 2012). Bryan, Paul C., ed.
JSON Reference: draft-pbryan-zyp-json-ref-03. Inter-
net Engineering Task Force.
12 External links
Format home page
The JavaScript Object Notation (JSON) Data Inter-
change Format (RFC 7159)
ECMA-404The JSON Data Interchange Format
13.2 Images
File:Commons-logo.svg Source: https://upload.wikimedia.org/wikipedia/en/4/4a/Commons-logo.svg License: PD Contributors: ? Origi-
nal artist: ?
File:Douglas_Crockford.jpg Source: https://upload.wikimedia.org/wikipedia/commons/e/ef/Douglas_Crockford.jpg License: CC BY-
SA 2.0 Contributors: Douglas Crockford - Yahoo Original artist: Franco Folini from San Francisco, USA
File:JSON_vector_logo.svg Source: https://upload.wikimedia.org/wikipedia/commons/c/c9/JSON_vector_logo.svg License: Public
domain Contributors: http://clipartist.info//openclipart.org/2011/Sept/September/06-Tuesday/JSON_Logo.svg Original artist: Douglas
Crockford
File:Lock-green.svg Source: https://upload.wikimedia.org/wikipedia/commons/6/65/Lock-green.svg License: CC0 Contributors: en:File:
Free-to-read_lock_75.svg Original artist: User:Trappist the monk
File:Wikibooks-logo.svg Source: https://upload.wikimedia.org/wikipedia/commons/f/fa/Wikibooks-logo.svg License: CC BY-SA 3.0
Contributors: Own work Original artist: User:Bastique, User:Ramac et al.
File:Wiktionary-logo-v2.svg Source: https://upload.wikimedia.org/wikipedia/commons/0/06/Wiktionary-logo-v2.svg License: CC BY-
SA 4.0 Contributors: Own work Original artist: Dan Polansky based on work currently attributed to Wikimedia Foundation but originally
created by Smurrayinchester
10 13 TEXT AND IMAGE SOURCES, CONTRIBUTORS, AND LICENSES