RFC 7158
The JavaScript Object Notation (JSON) Data Interchange Format
RFC 7158 πβ¨
Internet Engineering Task Force (IETF) π Request for Comments: 7158 π Category: Standards Track πΈ ISSN: 2070-1721 π¦
Okay bestie, the author is T. Bray, Ed. from Google, Inc. π It was published in March 2013 β¨ and it's a Standards Track doc, so this is the real deal, not just a suggestion π. It obsoletes RFC 4627 π (so 4627 is officially retired, RIP π―οΈ) and it also updates RFC 4627 π₯, which we'll get into in Section 1.2.
The title is The JavaScript Object Notation (JSON) Data Interchange Format π
Abstract π¦
JavaScript Object Notation (JSON) is a lightweight, text-based, language-independent data interchange format π. It was derived from the ECMAScript Programming Language Standard, and it defines a small set of formatting rules so structured data can be represented in a portable way π.
This document cleans up inconsistencies with other JSON specs, fixes some spec errors, and gives interoperability advice based on real-world experience π«Ά.
Status of This Memo πΈ
This is an Internet Standards Track document π.
It's a product of the Internet Engineering Task Force (IETF) and represents the consensus of the IETF community π. It got public review and was approved for publication by the Internet Engineering Steering Group (IESG) π . If you want more info on Internet Standards, check out Section 2 of RFC 5741 β¨.
For the current status, any errata, and how to send feedback, go to http://www.rfc-editor.org/info/rfc7158 π¦.
Copyright Notice π
Copyright (c) 2013 IETF Trust and the persons identified as the document authors. All rights reserved π.
This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (http://trustee.ietf.org/license-info) as they stand on the publication date π. Read these carefully, because they spell out your rights and restrictions for this document π. Code Components pulled out of this document must include Simplified BSD License text, as described in Section 4.e of the Trust Legal Provisions, and they come with no warranty, as the Simplified BSD License describes π₯.
This document may contain material from IETF Documents or IETF Contributions that were published or made publicly available before November 10, 2008 π. The person(s) controlling the copyright in some of that material may not have given the IETF Trust the right to allow modifications outside the IETF Standards Process π. So unless you get an adequate license from whoever controls that copyright, this document can't be modified outside the IETF Standards Process, and derivative works can't be made outside it either. The one exceptions are formatting it for publication as an RFC or translating it into languages other than English πΈ.
Table of Contents π§
1. Introduction ....................................................3
1.1. Conventions Used in This Document ..........................4
1.2. Specifications of JSON .....................................4
1.3. Introduction to This Revision ..............................4
2. JSON Grammar ....................................................4
3. Values ..........................................................5
4. Objects .........................................................6
5. Arrays ..........................................................6
6. Numbers .........................................................6
7. Strings .........................................................8
8. String and Character Issues .....................................9
8.1. Character Encoding .........................................9
8.2. Unicode Characters .........................................9
8.3. String Comparison ..........................................9
9. Parsers ........................................................10
10. Generators ....................................................10
11. IANA Considerations ...........................................10
12. Security Considerations .......................................11
13. Examples ......................................................12
14. Contributors ..................................................13
15. References ....................................................13
15.1. Normative References .....................................13
15.2. Informative References ...................................13
Appendix A. Changes from RFC 4627 .................................15
1. Introduction πΈ
JSON, short for JavaScript Object Notation, is a text format for serializing structured data π. It's derived from the object literals of JavaScript, as defined in the ECMAScript Programming Language Standard, Third Edition [ECMA-262] β¨.
JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays) π.
A string is a sequence of zero or more Unicode characters [UNICODE] π¦. Note that this citation points to the latest version of Unicode rather than a specific release. Future changes to the UNICODE specification are not expected to affect the syntax of JSON π«Ά.
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array π .
An array is an ordered sequence of zero or more values π.
The terms "object" and "array" come from JavaScript conventions π§.
JSON's design goals were to be minimal, portable, textual, and a subset of JavaScript π.
1.1. Conventions Used in This Document π
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119] π₯.
The grammatical rules in this document are to be interpreted as described in [RFC5234] π.
1.2. Specifications of JSON π
This document updates [RFC4627], which describes JSON and registers the media type "application/json" π.
A description of JSON in ECMAScript terms appears in Version 5.1 of the ECMAScript specification [ECMA-262], Section 15.12. JSON is also described in [ECMA-404] β¨.
All of the specifications of JSON syntax agree on the syntactic elements of the language π¦.
1.3. Introduction to This Revision πΈ
Since RFC 4627 came out, JSON has gotten super widely used π. That experience has shown some patterns that, while allowed by its specifications, have caused interoperability problems π.
A few errata have also been reported (see RFC Errata IDs 607 [Err607] and 3607 [Err3607]) π.
So this document aims to apply those errata, remove inconsistencies with other JSON specifications, and call out practices that can lead to interoperability problems π«Ά.
2. JSON Grammar π
A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names π§.
A JSON text is a serialized value. Certain earlier JSON specs required a JSON text to be an object or an array. Implementations that generate only objects or arrays wherever a JSON text is called for will still be interoperable, since all implementations will accept these as conforming JSON texts π .
JSON-text = ws value ws
These are the six structural characters:
begin-array = ws %x5B ws ; [ left square bracket
begin-object = ws %x7B ws ; { left curly bracket
end-array = ws %x5D ws ; ] right square bracket
end-object = ws %x7D ws ; } right curly bracket
name-separator = ws %x3A ws ; : colon
value-separator = ws %x2C ws ; , comma
Insignificant whitespace is allowed before or after any of the six structural characters π.
ws = *(
%x20 / ; Space
%x09 / ; Horizontal tab
%x0A / ; Line feed or New line
%x0D ) ; Carriage return
3. Values π
A JSON value MUST be an object, array, number, or string, or one of the following three literal names π₯:
false null true
The literal names MUST be lowercase π. No other literal names are allowed π.
value = false / null / true / object / array / number / string
false = %x66.61.6c.73.65 ; false
null = %x6e.75.6c.6c ; null
true = %x74.72.75.65 ; true
4. Objects π«Ά
An object is written as a pair of curly brackets surrounding zero or more name/value pairs (also called members) π¦. A name is a string β¨. A single colon comes after each name to separate it from its value πΈ. A single comma separates a value from the name that follows it π. The names within an object SHOULD be unique π.
object = begin-object [ member *( value-separator member ) ]
end-object
member = string name-separator value
An object whose names are all unique is interoperable, meaning every software implementation that receives it will agree on the name-value mappings π. When the names within an object are not unique, how software receiving that object behaves is unpredictable π. Many implementations report only the last name/value pair. Other implementations report an error or fail to parse the object. Some implementations report all of the name/value pairs, duplicates included π.
JSON parsing libraries have been observed to differ on whether they expose the ordering of object members to calling software π§. Implementations whose behavior doesn't depend on member ordering will be interoperable, because these differences won't affect them π.
5. Arrays π
An array is written as square brackets surrounding zero or more values (also called elements) π. Elements are separated by commas π.
array = begin-array [ value *( value-separator value ) ] end-array
There is no requirement that the values in an array be of the same type πΈβ¨.
6. Numbers π’β¨
okay so numbers in JSON are giving the same energy as most programming languages π they're written in base 10 using decimal digits π a number has an integer part that can start with an optional minus sign π€ and then it can also have a fraction part and/or an exponent part, bc why not π¦ leading zeros are not allowed π ββοΈ
a fraction part is a decimal point followed by one or more digits πΈ
an exponent part starts with the letter E, upper or lower case π it can be followed by a plus or minus sign, and then you need one or more digits after the E and the optional sign β¨
numeric values that can't be written in the grammar below (like Infinity and NaN) are not permitted π«
number = [ minus ] int [ frac ] [ exp ]
decimal-point = %x2E ; .
digit1-9 = %x31-39 ; 1-9
e = %x65 / %x45 ; e E
exp = e [ minus / plus ] 1*DIGIT
frac = decimal-point 1*DIGIT
int = zero / ( digit1-9 *DIGIT )
minus = %x2D ; -
plus = %x2B ; +
zero = %x30 ; 0
this spec lets implementations set limits on the range and precision of the numbers they accept π software that implements IEEE 754-2008 binary64 (double precision) numbers [IEEE754] is generally available and widely used, so good interoperability can be achieved by implementations that expect no more precision or range than those provide. in other words, implementations will approximate JSON numbers within the expected precision π§
β οΈ a JSON number like1E400or3.141592653589793238462643383279may cause interop drama π it suggests the software that made it expects the receiving software to have way more numeric magnitude and precision than is widely available. not it, bestie π
when that kind of software is used, numbers that are integers and fall in the range [-(2**53)+1, (2**53)-1] are interoperable, because implementations will agree exactly on their numeric values π that's the sweet spot π
7. Strings π
strings are similar to the conventions used in the C family of programming languages π a string begins and ends with quotation marks. all Unicode characters can go inside the quotation marks, except for the characters that must be escaped: the quotation mark, the reverse solidus, and the control characters (U+0000 through U+001F) π«Ά
any character may be escaped π¦ if the character is in the Basic Multilingual Plane (U+0000 through U+FFFF), it can be written as a six-character sequence: a reverse solidus, then the lowercase letter u, then four hexadecimal digits that encode the code point. the hex letters A through F can be upper or lower case β¨ so for example, a string with just one reverse solidus character can be written as "\u005C" π
there are also two-character escape sequences for some popular characters π so a string with just one reverse solidus can be written more compactly as "\\" π
to escape an extended character that is not in the Basic Multilingual Plane, you write it as a 12-character sequence that encodes the UTF-16 surrogate pair πΈ for example, a string with only the G clef character (U+1D11E) can be written as "\uD834\uDD1E" πΌ
string = quotation-mark *char quotation-mark
char = unescaped /
escape (
%x22 / ; " quotation mark U+0022
%x5C / ; \ reverse solidus U+005C
%x2F / ; / solidus U+002F
%x62 / ; b backspace U+0008
%x66 / ; f form feed U+000C
%x6E / ; n line feed U+000A
%x72 / ; r carriage return U+000D
%x74 / ; t tab U+0009
%x75 4HEXDIG ) ; uXXXX U+XXXX
escape = %x5C ; \
quotation-mark = %x22 ; "
unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
8. String and Character Issues π§΅
8.1. Character Encoding π€
JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32. the default is UTF-8, and JSON texts encoded in UTF-8 are interoperable in the sense that they'll be read successfully by the maximum number of implementations π there are lots of implementations that can't read texts in other encodings (like UTF-16 and UTF-32), so no cap, UTF-8 is the move π
Implementations MUST NOT add a byte order mark to the beginning of a JSON text π« for the sake of interoperability, implementations that parse JSON texts MAY ignore the presence of a byte order mark instead of treating it as an error π«§
8.2. Unicode Characters π
when all the strings in a JSON text are made up entirely of Unicode characters [UNICODE] (however they're escaped), that JSON text is interoperable π all software implementations that parse it will agree on the contents of names and of string values in objects and arrays π₯°
however, the ABNF in this spec allows member names and string values to contain bit sequences that can't encode Unicode characters π¬ for example, "\uDEAD" (a single unpaired UTF-16 surrogate). this has been observed, for instance, when a library truncates a UTF-16 string without checking whether the truncation split a surrogate pair π the behavior of software that receives JSON texts with such values is unpredictable. implementations might return different values for the length of a string value, or even suffer fatal runtime exceptions π
8.3. String Comparison π
software implementations are typically required to test names of object members for equality π§ implementations that convert the textual representation into sequences of Unicode code units and then compare them numerically, code unit by code unit, are interoperable in the sense that they'll agree in all cases on equality or inequality of two strings β¨ for example, implementations that compare strings with escaped characters unconverted may incorrectly find that "a\\b" and "a\u005Cb" are not equal π
9. Parsers π§
a JSON parser transforms a JSON text into another representation π¦ a JSON parser MUST accept all texts that conform to the JSON grammar. a JSON parser MAY accept non-JSON forms or extensions π
an implementation may set limits on the size of texts that it accepts π it may set limits on the maximum depth of nesting πͺ it may set limits on the range and precision of numbers π’ and it may set limits on the length and character contents of strings π
10. Generators π¨οΈ
a JSON generator produces JSON text π and the resulting text MUST strictly conform to the JSON grammar. no vibes-based JSON allowed π ββοΈβ¨
11. IANA Considerations π
the MIME media type for JSON text is application/json π here's the whole registration, verbatim, bc it's a lot of fields π
Type name: application
Subtype name: json
Required parameters: n/a
Optional parameters: n/a
Encoding considerations: binary
Security considerations: See [RFC7158], Section 12.
Interoperability considerations: Described in [RFC7158]
Published specification: [RFC7158]
Applications that use this media type:
JSON has been used to exchange data between applications written
in all of these programming languages: ActionScript, C, C#,
Clojure, ColdFusion, Common Lisp, E, Erlang, Go, Java, JavaScript,
Lua, Objective CAML, Perl, PHP, Python, Rebol, Ruby, Scala, and
Scheme.
Additional information:
Magic number(s): n/a
File extension(s): .json
Macintosh file type code(s): TEXT
Person & email address to contact for further information:
IESG
<iesg@ietf.org>
Intended usage: COMMON
Restrictions on usage: none
Author:
Douglas Crockford
<douglas@crockford.com>
Change controller:
IESG
<iesg@ietf.org>
Note: No "charset" parameter is defined for this registration.
Adding one really has no effect on compliant recipients.
12. Security Considerations π¨
π¨ okay bestie, this one is SERIOUS. generally, there are security issues with scripting languages π± JSON is a subset of JavaScript, but it excludes assignment and invocation π‘οΈ
since JSON's syntax is borrowed from JavaScript, it's possible to use that language's eval() function to parse JSON texts π this generally constitutes an unacceptable security risk, because the text could contain executable code along with the data declarations π
ββοΈ the same consideration applies to using eval()-like functions in any other programming language where JSON texts conform to that language's syntax π«
13. Examples π
here's a JSON object πΌοΈ
{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": 100
},
"Animated" : false,
"IDs": [116, 943, 234, 38793]
}
}
its Image member is an object whose Thumbnail member is an object, and whose IDs member is an array of numbers πΈ
and here's a JSON array holding two objects π
[
{
"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959,
"Address": "",
"City": "SAN FRANCISCO",
"State": "CA",
"Zip": "94107",
"Country": "US"
},
{
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.026020,
"Address": "",
"City": "SUNNYVALE",
"State": "CA",
"Zip": "94085",
"Country": "US"
}
]
and last but not least, three small JSON texts that contain only values β¨
"Hello world!"
42
true
14. Contributors π«Ά
RFC 4627 was written by Douglas Crockford π this document was built by making a relatively small number of changes to that document, so the vast majority of the text here is his π the main character of this whole spec is, iykyk π
15. References π
15.1. Normative References π
[IEEE754] IEEE, "IEEE Standard for Floating-Point Arithmetic", IEEE
Standard 754, August 2008,
<http://grouper.ieee.org/groups/754/>.
[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
Requirement Levels", BCP 14, RFC 2119, March 1997.
[RFC5234] Crocker, D. and P. Overell, "Augmented BNF for Syntax
Specifications: ABNF", STD 68, RFC 5234, January 2008.
[UNICODE] The Unicode Consortium, "The Unicode Standard",
<http://www.unicode.org/versions/latest/>.
15.2. Informative References π¦
[ECMA-262] Ecma International, "ECMAScript Language Specification
Edition 5.1", Standard ECMA-262, June 2011,
<http://www.ecma-international.org/publications/standards/
Ecma-262.htm>.
[ECMA-404] Ecma International, "The JSON Data Interchange Format",
Standard ECMA-404, October 2013,
<http://www.ecma-international.org/publications/standards/
Ecma-404.htm>.
[Err3607] RFC Errata, Errata ID 3607, RFC 3607,
<http://www.rfc-editor.org>.
[Err607] RFC Errata, Errata ID 607, RFC 607,
<http://www.rfc-editor.org>.
[RFC4627] Crockford, D., "The application/json Media Type for
JavaScript Object Notation (JSON)", RFC 4627, July 2006.
Appendix A. Changes from RFC 4627 β¨
Okay so this section is basically the changelog for the whole doc, and honestly it's giving glow-up π¦ It lists every change between this document and the text in RFC 4627, so if you're wondering "wait, what changed since last time?" this is where the tea is β.
- Changed the title and abstract of the document. π
- Changed the reference to [UNICODE] to be not version specific. π
- Added a "Specifications of JSON" section. π
- Added an "Introduction to This Revision" section. π
- Changed the definition of "JSON text" so that it can be any JSON value, removing the constraint that it be an object or array. π This one is so iconic, a JSON text doesn't have to be an object or array anymore!!
- Added language about duplicate object member names, member ordering, and interoperability. π«Ά
- Clarified the absence of a requirement that values in an array be of the same JSON type. π§ Arrays can be a mixed bag, bestie, no rules there.
- Applied erratum #607 from RFC 4627 to correctly align the artwork for the definition of "object". π
- Changed "as sequences of digits" to "in the grammar below" in the "Numbers" section, and made base-10-ness explicit. π₯
- Added language about number interoperability as a function of IEEE754, and added an IEEE754 reference. πΈ
- Added language about interoperability and Unicode characters and about string comparisons. To do this, turned the old "Encoding" section into a "String and Character Issues" section, with three subsections: "Character Encoding", "Unicode Characters", and "String Comparison". π That's the reorg arc, periodt.
- Changed guidance in the "Parsers" section to point out that implementations may set limits on the range "and precision" of numbers. π¦
- Updated and tidied the "IANA Considerations" section. β¨
- Made a real "Security Considerations" section and lifted the text out of the previous "IANA Considerations" section. ππ
- Applied erratum #3607 from RFC 4627 by removing the security consideration that begins "A JSON text can be safely passed" and the JavaScript code that went with that consideration. π Bye bye, that one.
- Added a note to the "Security Considerations" section pointing out the risks of using the "eval()" function in JavaScript or any other language in which JSON texts conform to that language's syntax. β οΈ eval() is not your friend, besties.
- Added a note to the "IANA Considerations" clarifying the absence of a "charset" parameter for the application/json media type. π
- Changed "100" to 100 and added a boolean field, both in the first example. π
- Added examples of JSON texts with simple values, neither objects nor arrays. π
- Added a "Contributors" section crediting Douglas Crockford. π«Ά Shoutout to him, obsessed!
- Added a reference to RFC 4627. π
- Moved the ECMAScript reference from Normative to Informative and updated it to reference ECMAScript 5.1, and added a reference to ECMA 404. π
Author's Address π
Tim Bray (editor) Google, Inc.
EMail: tbray@textuality.com π
π¦ where this page comes from
- ietf: rfc7158 2014-03
- π girlified by claude-haiku-5-5 on : a language model's rewrite of the original, under its terms