1e41f4b71Sopenharmony_ci# @ohos.util.json (JSON Parsing and Generation)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe JSON module provides a series of APIs for converting JSON text into JSON objects or values and converting objects into JSON strings.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci>**NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci>The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## Modules to Import
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci```ts
13e41f4b71Sopenharmony_ciimport { JSON } from '@kit.ArkTS';
14e41f4b71Sopenharmony_ci```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## Transformer
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_citype Transformer = (this: Object, key: string, value: Object) => Object | undefined | null
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ciDefines the type of the conversion result function.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**Parameters**
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description           |
29e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------- |
30e41f4b71Sopenharmony_ci| this   | Object | Yes| Object to which the key-value pair to parse belongs.|
31e41f4b71Sopenharmony_ci| key  | string | Yes| Key to parse.|
32e41f4b71Sopenharmony_ci| value  | Object | Yes| Value of the key.|
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci**Returns**
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci| Type| Description|
37e41f4b71Sopenharmony_ci| -------- | -------- |
38e41f4b71Sopenharmony_ci| Object \| undefined \| null | Object obtained after parsing, undefined, or null.|
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci## BigIntMode
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ciEnumerates the modes for processing BigInt.
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci| Name| Value| Description           |
49e41f4b71Sopenharmony_ci| ------ | ------ | --------------- |
50e41f4b71Sopenharmony_ci| DEFAULT   | 0 |BigInt is not supported.|
51e41f4b71Sopenharmony_ci| PARSE_AS_BIGINT   | 1 |Parses an integer that is less than -(2^53-1) or greater than (2^53-1) as BigInt.|
52e41f4b71Sopenharmony_ci| ALWAYS_PARSE_AS_BIGINT   | 2 |Parses all integers as BigInt.|
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci## ParseOptions
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ciDescribes the parsing options, which can define the mode for processing BigInt.
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci| Name| Type| Mandatory|Description           |
63e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------- |
64e41f4b71Sopenharmony_ci| bigIntMode   | [BigIntMode](#bigintmode) | Yes|Mode for processing BigInt.|
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci## JSON.parse
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ciparse(text: string, reviver?: Transformer, options?: ParseOptions): Object | null
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ciParses a JSON string into an ArkTS object or null.
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci**Parameters**
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description           |
79e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------- |
80e41f4b71Sopenharmony_ci| text   | string | Yes| Valid JSON string.|
81e41f4b71Sopenharmony_ci| reviver  | [Transformer](#transformer) | No| Conversion function. This parameter can be used to modify the value generated after parsing. The default value is undefined.|
82e41f4b71Sopenharmony_ci| options   | [ParseOptions](#parseoptions) | No| Parsing options. This parameter is used to control the type of the parsing result. The default value is undefined.|
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci**Returns**
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci| Type| Description|
87e41f4b71Sopenharmony_ci| -------- | -------- |
88e41f4b71Sopenharmony_ci| Object \| null | ArkTS object or null (if null is passed in).|
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci**Error codes**
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci| ID| Error Message|
95e41f4b71Sopenharmony_ci| -------- | -------- |
96e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci**Example**
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci```ts
101e41f4b71Sopenharmony_cilet jsonText = '{"name": "John", "age": 30, "city": "ChongQing"}';
102e41f4b71Sopenharmony_cilet obj = JSON.parse(jsonText);
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_cilet options: JSON.ParseOptions = {
105e41f4b71Sopenharmony_ci  bigIntMode: JSON.BigIntMode.PARSE_AS_BIGINT,
106e41f4b71Sopenharmony_ci}
107e41f4b71Sopenharmony_cilet numberText = '{"largeNumber":112233445566778899}';
108e41f4b71Sopenharmony_cilet numberObj = JSON.parse(numberText,(key: string, value: Object | undefined | null): Object | undefined | null => {
109e41f4b71Sopenharmony_ci  if(key === "largeNumber") return value;
110e41f4b71Sopenharmony_ci  return value;
111e41f4b71Sopenharmony_ci},options) as Object;
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ciconsole.info((numberObj as object)?.["largeNumber"]);
114e41f4b71Sopenharmony_ci// Expected output: 112233445566778899
115e41f4b71Sopenharmony_ci```
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci## JSON.stringify
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_cistringify(value: Object, replacer?: (number | string)[] | null, space?: string | number): string
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ciConverts an ArkTS object or array into a JSON string. In the case of a container, linear containers are supported, but non-linear containers are not.
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci**Parameters**
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
131e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
132e41f4b71Sopenharmony_ci| value | Object | Yes| ArkTS object or array. In the case of a container, linear containers are supported, but non-linear containers are not.|
133e41f4b71Sopenharmony_ci| replacer | number[] \| string[] \| null | No| If an array is passed in, only the keys in the array are serialized to the final JSON string. If null is passed in, all keys of the object are serialized. The default value is undefined.|
134e41f4b71Sopenharmony_ci| space | string \| number | No| Indentation, white space, or line break characters inserted into the output JSON string for readability purposes. If a number is passed in, it indicates the number of space characters to be used as indentation. If a string is passed in, the string is inserted before the output JSON string. If null is passed in, no white space is used. The default value is an empty string.|
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci**Returns**
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci| Type| Description|
139e41f4b71Sopenharmony_ci| -------- | -------- |
140e41f4b71Sopenharmony_ci| string | JSON string after conversion.|
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci**Error codes**
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci| ID| Error Message|
147e41f4b71Sopenharmony_ci| -------- | -------- |
148e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**Example**
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci```ts
153e41f4b71Sopenharmony_ciinterface Person {
154e41f4b71Sopenharmony_ci  name: string;
155e41f4b71Sopenharmony_ci  age: number;
156e41f4b71Sopenharmony_ci  city: string;
157e41f4b71Sopenharmony_ci}
158e41f4b71Sopenharmony_cilet obj = {"name": "John", "age": 30, "city": "ChongQing"} as Person;
159e41f4b71Sopenharmony_cilet str1 = JSON.stringify(obj, ["name"]);
160e41f4b71Sopenharmony_ci```
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci## JSON.stringify
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_cistringify(value: Object, replacer?: Transformer, space?: string | number): string
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ciConverts an ArkTS object or array into a JSON string. In the case of a container, linear containers are supported, but non-linear containers are not.
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci**Parameters**
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
176e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
177e41f4b71Sopenharmony_ci| value | Object | Yes| ArkTS object or array. In the case of a container, linear containers are supported, but non-linear containers are not.|
178e41f4b71Sopenharmony_ci| replacer | [Transformer](#transformer) | No| During serialization, each key of the serialized value is converted and processed by this function. The default value is undefined.|
179e41f4b71Sopenharmony_ci| space | string \| number | No| Indentation, white space, or line break characters inserted into the output JSON string for readability purposes. If a number is passed in, it indicates the number of space characters to be used as indentation. If a string is passed in, the string is inserted before the output JSON string. If null is passed in, no white space is used. The default value is an empty string.|
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci**Returns**
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci| Type| Description|
184e41f4b71Sopenharmony_ci| -------- | -------- |
185e41f4b71Sopenharmony_ci| string | JSON string after conversion.|
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci**Error codes**
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci| ID| Error Message|
192e41f4b71Sopenharmony_ci| -------- | -------- |
193e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci**Example**
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci```ts
198e41f4b71Sopenharmony_cifunction replacer(key: string, value: Object): Object {
199e41f4b71Sopenharmony_ci  if (typeof value === "string") {
200e41f4b71Sopenharmony_ci    return value.toUpperCase();
201e41f4b71Sopenharmony_ci  }
202e41f4b71Sopenharmony_ci  return value;
203e41f4b71Sopenharmony_ci}
204e41f4b71Sopenharmony_ciinterface Person {
205e41f4b71Sopenharmony_ci  name: string;
206e41f4b71Sopenharmony_ci  age: number;
207e41f4b71Sopenharmony_ci  city: string;
208e41f4b71Sopenharmony_ci}
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_cilet obj = {"name": "John", "age": 30, "city": "ChongQing"} as Person;
211e41f4b71Sopenharmony_cilet str2 = JSON.stringify(obj, replacer);
212e41f4b71Sopenharmony_ci```
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ci## JSON.has
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_cihas(obj: object, property: string): boolean
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_ciChecks whether an ArkTS object contains a key. This API can be used for related operations after [JSON.parse](#jsonparse) is called to parse a JSON string. This API supports only valid JSON strings whose outermost layer is in dictionary format (in braces instead of square brackets).
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci**Parameters**
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
228e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
229e41f4b71Sopenharmony_ci| obj | object | Yes| ArkTS object.|
230e41f4b71Sopenharmony_ci| property | string | Yes| Key to check.|
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci**Returns**
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci| Type| Description|
235e41f4b71Sopenharmony_ci| -------- | -------- |
236e41f4b71Sopenharmony_ci| boolean | **true**: The ArkTS object contains the key.<br>**false**: The ArkTS object does not contain the key.|
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci**Error codes**
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci| ID| Error Message|
243e41f4b71Sopenharmony_ci| -------- | -------- |
244e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci**Example**
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci```ts
249e41f4b71Sopenharmony_ciconst jsonText = '{"name": "John", "age": 30, "city": "ChongQing"}';
250e41f4b71Sopenharmony_cilet obj = JSON.parse(jsonText);
251e41f4b71Sopenharmony_cilet rst = JSON.has(obj, "name");
252e41f4b71Sopenharmony_ci```
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci## JSON.remove
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_ciremove(obj: object, property: string): void
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ciRemoves a key from an ArkTS object. This API can be used for related operations after [JSON.parse](#jsonparse) is called to parse a JSON string. This API supports only valid JSON strings whose outermost layer is in dictionary format (in braces instead of square brackets).
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci**Parameters**
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
268e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
269e41f4b71Sopenharmony_ci| obj | object | Yes| ArkTS object.|
270e41f4b71Sopenharmony_ci| property | string | Yes| Key to remove.|
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci**Error codes**
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ci| ID| Error Message|
277e41f4b71Sopenharmony_ci| -------- | -------- |
278e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci**Example**
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ci```ts
283e41f4b71Sopenharmony_ciconst jsonText = '{"name": "John", "age": 30, "city": "ChongQing"}';
284e41f4b71Sopenharmony_cilet obj = JSON.parse(jsonText);
285e41f4b71Sopenharmony_cilet rst = JSON.remove(obj, "name");
286e41f4b71Sopenharmony_ci```
287