Kebab file names
This commit is contained in:
28
snippets/is-valid-json.md
Normal file
28
snippets/is-valid-json.md
Normal file
@ -0,0 +1,28 @@
|
||||
---
|
||||
title: String is valid JSON
|
||||
tags: type
|
||||
cover: italian-horizon
|
||||
firstSeen: 2017-12-31T14:53:01+02:00
|
||||
lastUpdated: 2020-10-18T13:49:49+03:00
|
||||
---
|
||||
|
||||
Checks if the provided string is a valid JSON.
|
||||
|
||||
- Use `JSON.parse()` and a `try...catch` block to check if the provided string is a valid JSON.
|
||||
|
||||
```js
|
||||
const isValidJSON = str => {
|
||||
try {
|
||||
JSON.parse(str);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
isValidJSON('{"name":"Adam","age":20}'); // true
|
||||
isValidJSON('{"name":"Adam",age:"20"}'); // false
|
||||
isValidJSON(null); // true
|
||||
```
|
||||
Reference in New Issue
Block a user