README.md (3085B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Parse JSON 22 23 > Parse a string as [JSON][json]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var parseJSON = require( '@stdlib/utils/parse-json' ); 31 ``` 32 33 #### parseJSON( str\[, reviver] ) 34 35 Parses a `string` as [JSON][json]. 36 37 ```javascript 38 var out = parseJSON( '{"beep":"boop"}' ); 39 // returns {'beep':'boop'} 40 ``` 41 42 If unable to parse a `string` as [JSON][json], the function returns an error. 43 44 ```javascript 45 var out = parseJSON( 'beep' ); 46 // returns <SyntaxError> 47 ``` 48 49 To transform the `string` being parsed, provide a `reviver`. 50 51 ```javascript 52 function reviver( key, value ) { 53 if ( key === '' ) { 54 return value; 55 } 56 if ( key === 'beep' ) { 57 return value; 58 } 59 } 60 61 var str = '{"beep":"boop","a":"b"}'; 62 var out = parseJSON( str, reviver ); 63 // returns {'beep':'boop'} 64 ``` 65 66 </section> 67 68 <!-- /.usage --> 69 70 <section class="notes"> 71 72 ## Notes 73 74 - In contrast to the native [`JSON.parse()`][json-parse], this implementation throws a `TypeError` if provided **any** value which is not a `string`. 75 76 ```javascript 77 var out = JSON.parse( null ); 78 // returns null 79 80 out = parseJSON( null ); 81 // throws <TypeError> 82 ``` 83 84 - In contrast to the native [`JSON.parse()`][json-parse], this implementation does **not** throw a `SyntaxError` if unable to parse a `string` as [JSON][json]. 85 86 ```javascript 87 var out = parseJSON( '{"beep":"boop}' ); 88 // returns <SyntaxError> 89 90 out = JSON.parse( '{"beep":"boop}' ); 91 // throws <SyntaxError> 92 ``` 93 94 - In contrast to the native [`JSON.parse()`][json-parse], this implementation throws a `TypeError` if provided a `reviver` argument which is **not** a `function`. 95 96 ```javascript 97 var out = JSON.parse( '{"a":"b"}', [] ); 98 // returns {'a':'b'} 99 100 out = parseJSON( '{"a":"b"}', [] ); 101 // throws <TypeError> 102 ``` 103 104 </section> 105 106 <!-- /.notes --> 107 108 <section class="examples"> 109 110 ## Examples 111 112 <!-- eslint no-undef: "error" --> 113 114 ```javascript 115 var parseJSON = require( '@stdlib/utils/parse-json' ); 116 117 var out; 118 119 out = parseJSON( '{"beep":"boop"}' ); 120 // returns {'beep':'boop'} 121 122 out = parseJSON( '3.14' ); 123 // returns 3.14 124 125 out = parseJSON( 'true' ); 126 // returns true 127 128 out = parseJSON( 'null' ); 129 // returns null 130 131 out = parseJSON( '{"beep":"boop}' ); 132 // returns <SyntaxError> 133 ``` 134 135 </section> 136 137 <!-- /.examples --> 138 139 <section class="links"> 140 141 [json]: http://www.json.org/ 142 143 [json-parse]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse 144 145 </section> 146 147 <!-- /.links -->