aboutsummaryrefslogtreecommitdiff
path: root/files/it/web/javascript/reference/global_objects/json/index.html
blob: caa08b766f745957d0109ae9d9a1e521c4d3cbcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
---
title: JSON
slug: Web/JavaScript/Reference/Global_Objects/JSON
tags:
  - JSON
  - JavaScript
  - NeedsTranslation
  - Object
  - Reference
  - TopicStub
  - polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/JSON
---
<div>{{JSRef}}</div>

<p>The <strong><code>JSON</code></strong> object contains methods for parsing <a class="external" href="http://json.org/">JavaScript Object Notation</a> ({{glossary("JSON")}}) and converting values to JSON. It can't be called or constructed, and aside from its two method properties it has no interesting functionality of its own.</p>

<h2 id="Description">Description</h2>

<h3 id="JavaScript_Object_Notation">JavaScript Object Notation</h3>

<p>JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and {{jsxref("null")}}. It is based upon JavaScript syntax but is distinct from it: some JavaScript is not JSON, and some JSON is not JavaScript. See also <a href="http://timelessrepo.com/json-isnt-a-javascript-subset">JSON: The JavaScript subset that isn't</a>.</p>

<table>
 <caption>JavaScript and JSON differences</caption>
 <thead>
  <tr>
   <th scope="col">JavaScript type</th>
   <th scope="col">JSON differences</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>Objects and Arrays</td>
   <td>Property names must be double-quoted strings; trailing commas are forbidden.</td>
  </tr>
  <tr>
   <td>Numbers</td>
   <td>Leading zeros are prohibited; a decimal point must be followed by at least one digit.</td>
  </tr>
  <tr>
   <td>Strings</td>
   <td>
    <p>Only a limited sets of characters may be escaped; certain control characters are prohibited; the Unicode line separator (<a href="http://unicode-table.com/en/2028/">U+2028</a>) and paragraph separator (<a href="http://unicode-table.com/en/2029/">U+2029</a>) characters are permitted; strings must be double-quoted. See the following example where {{jsxref("JSON.parse()")}} works fine and a {{jsxref("SyntaxError")}} is thrown when evaluating the code as JavaScript:</p>

    <pre class="brush: js">
var code = '"\u2028\u2029"';
JSON.parse(code); // works fine
eval(code); // fails
</pre>
   </td>
  </tr>
 </tbody>
</table>

<p>The full JSON syntax is as follows:</p>

<pre><var>JSON</var> = <strong>null</strong>
    <em>or</em> <strong>true</strong> <em>or</em> <strong>false</strong>
    <em>or</em> <var>JSONNumber</var>
    <em>or</em> <var>JSONString</var>
    <em>or</em> <var>JSONObject</var>
    <em>or</em> <var>JSONArray</var>

<var>JSONNumber</var> = <strong>-</strong> <var>PositiveNumber</var>
          <em>or</em> <var>PositiveNumber</var>
<var>PositiveNumber</var> = DecimalNumber
              <em>or</em> <var>DecimalNumber</var> <strong>.</strong> <var>Digits</var>
              <em>or</em> <var>DecimalNumber</var> <strong>.</strong> <var>Digits</var> <var>ExponentPart</var>
              <em>or</em> <var>DecimalNumber</var> <var>ExponentPart</var>
<var>DecimalNumber</var> = <strong>0</strong>
             <em>or</em> <var>OneToNine</var> <var>Digits</var>
<var>ExponentPart</var> = <strong>e</strong> <var>Exponent</var>
            <em>or</em> <strong>E</strong> <var>Exponent</var>
<var>Exponent</var> = <var>Digits</var>
        <em>or</em> <strong>+</strong> <var>Digits</var>
        <em>or</em> <strong>-</strong> <var>Digits</var>
<var>Digits</var> = <var>Digit</var>
      <em>or</em> <var>Digits</var> <var>Digit</var>
<var>Digit</var> = <strong>0</strong> through <strong>9</strong>
<var>OneToNine</var> = <strong>1</strong> through <strong>9</strong>

<var>JSONString</var> = <strong>""</strong>
          <em>or</em> <strong>"</strong> <var>StringCharacters</var> <strong>"</strong>
<var>StringCharacters</var> = <var>StringCharacter</var>
                <em>or</em> <var>StringCharacters</var> <var>StringCharacter</var>
<var>StringCharacter</var> = any character
                  <em>except</em> <strong>"</strong> <em>or</em> <strong>\</strong> <em>or</em> U+0000 through U+001F
               <em>or</em> <var>EscapeSequence</var>
<var>EscapeSequence</var> = <strong>\"</strong> <em>or</em> <strong>\/</strong> <em>or</em> <strong>\\</strong> <em>or</em> <strong>\b</strong> <em>or</em> <strong>\f</strong> <em>or</em> <strong>\n</strong> <em>or</em> <strong>\r</strong> <em>or</em> <strong>\t</strong>
              <em>or</em> <strong>\u</strong> <var>HexDigit</var> <var>HexDigit</var> <var>HexDigit</var> <var>HexDigit</var>
<var>HexDigit</var> = <strong>0</strong> through <strong>9</strong>
        <em>or</em> <strong>A</strong> through <strong>F</strong>
        <em>or</em> <strong>a</strong> through <strong>f</strong>

<var>JSONObject</var> = <strong>{</strong> <strong>}</strong>
          <em>or</em> <strong>{</strong> <var>Members</var> <strong>}</strong>
<var>Members</var> = <var>JSONString</var> <strong>:</strong> <var>JSON</var>
       <em>or</em> <var>Members</var> <strong>,</strong> <var>JSONString</var> <strong>:</strong> <var>JSON</var>

<var>JSONArray</var> = <strong>[</strong> <strong>]</strong>
         <em>or</em> <strong>[</strong> <var>ArrayElements</var> <strong>]</strong>
<var>ArrayElements</var> = <var>JSON</var>
             <em>or</em> <var>ArrayElements</var> <strong>,</strong> <var>JSON</var>
</pre>

<p>Insignificant whitespace may be present anywhere except within a <code><var>JSONNumber</var></code> (numbers must contain no whitespace) or <code><var>JSONString</var></code> (where it is interpreted as the corresponding character in the string, or would cause an error). The tab character (<a href="http://unicode-table.com/en/0009/">U+0009</a>), carriage return (<a href="http://unicode-table.com/en/000D/">U+000D</a>), line feed (<a href="http://unicode-table.com/en/000A/">U+000A</a>), and space (<a href="http://unicode-table.com/en/0020/">U+0020</a>) characters are the only valid whitespace characters.</p>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{jsxref("JSON.parse()")}}</dt>
 <dd>Parse a string as JSON, optionally transform the produced value and its properties, and return the value.</dd>
 <dt>{{jsxref("JSON.stringify()")}}</dt>
 <dd>Return a JSON string corresponding to the specified value, optionally including only certain properties or replacing property values in a user-defined manner.</dd>
</dl>

<h2 id="Polyfill">Polyfill</h2>

<p>The <code>JSON</code> object is not supported in older browsers. You can work around this by inserting the following code at the beginning of your scripts, allowing use of <code>JSON</code> object in implementations which do not natively support it (like Internet Explorer 6).</p>

<p>The following algorithm is an imitation of the native <code>JSON</code> object:</p>

<pre class="brush: js">if (!window.JSON) {
  window.JSON = {
    parse: function(sJSON) { return eval('(' + sJSON + ')'); },
    stringify: (function () {
      var toString = Object.prototype.toString;
      var isArray = Array.isArray || function (a) { return toString.call(a) === '[object Array]'; };
      var escMap = {'"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'};
      var escFunc = function (m) { return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1); };
      var escRE = /[\\"\u0000-\u001F\u2028\u2029]/g;
      return function stringify(value) {
        if (value == null) {
          return 'null';
        } else if (typeof value === 'number') {
          return isFinite(value) ? value.toString() : 'null';
        } else if (typeof value === 'boolean') {
          return value.toString();
        } else if (typeof value === 'object') {
          if (typeof value.toJSON === 'function') {
            return stringify(value.toJSON());
          } else if (isArray(value)) {
            var res = '[';
            for (var i = 0; i &lt; value.length; i++)
              res += (i ? ', ' : '') + stringify(value[i]);
            return res + ']';
          } else if (toString.call(value) === '[object Object]') {
            var tmp = [];
            for (var k in value) {
              if (value.hasOwnProperty(k))
                tmp.push(stringify(k) + ': ' + stringify(value[k]));
            }
            return '{' + tmp.join(', ') + '}';
          }
        }
        return '"' + value.toString().replace(escRE, escFunc) + '"';
      };
    })()
  };
}
</pre>

<p>More complex well-known <a class="external" href="http://remysharp.com/2010/10/08/what-is-a-polyfill/">polyfills</a> for the <code>JSON</code> object are <a class="link-https" href="https://github.com/douglascrockford/JSON-js">JSON2</a> and <a class="external" href="http://bestiejs.github.com/json3">JSON3</a>.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.12', 'JSON')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-json-object', 'JSON')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("1.9.1")}}</td>
   <td>{{CompatIE("8.0")}}</td>
   <td>{{CompatOpera("10.5")}}</td>
   <td>{{CompatSafari("4.0")}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("1.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Date.prototype.toJSON()")}}</li>
</ul>