Check whether a string is a valid JSON

Check whether a string is a valid JSON

The isValidJson function takes a string as its parameter and uses the JSON.parse method to parse it. If the string is not a valid JSON, the function catches the error thrown by JSON.parse and returns false. Otherwise, it returns true.

In the example, the isValidJson function is called with two different strings: json1, which is a valid JSON, and json2, which is not a valid JSON because it uses single quotes instead of double quotes for the property names. The function returns true for json1 and false for json2. Thanks