Last updated: Mar 4, 2024
Reading timeยท3 min
Use the JSON.parse()
method to parse a JSON array, e.g. JSON.parse(arr)
.
The method parses a JSON string and returns its JavaScript value or object
equivalent.
If the provided parameter is not valid JSON, the JSON.parse
method throws a
SyntaxError
exception.
const jsonArr = '[{"id": 1, "name": "Tom"}, {"id": 2, "name": "Alice"}]'; const arr = JSON.parse(jsonArr); // ๐๏ธ [{id: 1, name: 'Tom'}, {id: 2, name: 'Alice'}] console.log(arr); console.log(Array.isArray(arr)); // ๐๏ธ true
We used the JSON.parse() method to parse a JSON array into its JavaScript equivalent.
The only parameter the JSON.parse()
method takes is the JSON string that
should be parsed.
You can use the Array.isArray()
method to verify that a value is a native
JavaScript array.
console.log(Array.isArray([])); // ๐๏ธ true console.log(Array.isArray('')); // ๐๏ธ false
A JSON value will always be of type string.
const arr = ['bobby', 'hadz', 'com']; const json = JSON.stringify(arr); console.log(json); // ๐๏ธ ["bobby","hadz","com"] console.log(typeof json); // ๐๏ธ string
If you provide an invalid JSON string to the method, a SyntaxError
exception
is thrown.
const invalidJson = '[{id: 1}]'; // โ๏ธ SyntaxError: Expected property name or '}' in JSON at position 2 console.log(JSON.parse(invalidJson));
id
property, which makes it invalid.The JSON.parse()
method attempts to parse the JSON but is unable to do so and
throws an exception.
try/catch
statement to handle a potential errorIf you need to handle this scenario, you can parse the JSON array inside of a
try/catch
block to make sure your program doesn't crash.
const jsonArr = '[{"id": 1, "name": "Tom"}, {"id": 2, "name": "Alice"}]'; let arr; try { arr = JSON.parse(jsonArr); console.log('โ JSON array parsed successfully'); } catch (err) { console.log('โ๏ธ invalid JSON provided'); // report error } // ๐๏ธ [{id: 1, name: 'Tom'}, {id: 2, name: 'Alice'}] console.log(arr);
If the provided JSON is invalid and our catch
block runs where we can handle
the error.
If the JSON is valid, it gets parsed and assigned to the arr
variable.
If your JSON is invalid, google for a JSON validator and paste it into the text box.
See what errors your JSON contains and correct them - either on the server or use the String.replace() or String.replaceAll() methods to correct them on the client.
For example, the following code replaces all single quotes with double quotes to produce a valid JSON array.
const invalidJson = "['apple', 'banana']"; const validJson = invalidJson.replaceAll(`'`, `"`); // ๐๏ธ {'apple', 'banana'} const parsed = JSON.parse(validJson); console.log(parsed);
String.replaceAll()
method to replace all occurrences of single quotes with double quotes to make the JSON array valid.The String.replaceAll() method returns a new string with all matches of a pattern replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A string used to replace the substring match by the supplied pattern. |
The String.replaceAll()
method returns a new string with the matches of the
pattern replaced. The method doesn't change the original string.
Strings are immutable in JavaScript.
You can learn more about the related topics by checking out the following tutorials: