-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.js
30 lines (26 loc) · 872 Bytes
/
json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function validateJSON(jsonString) {
try {
// Attempt to parse the JSON data
const jsonData = JSON.parse(jsonString);
// If parsing succeeds, return a success message
return { valid: true, error: null };
} catch (error) {
return { valid: false, error: error.message, position: error.at };
}
}
const jsonDataToValidate = `{
"name": "John Doe",
"age": 30,
"email": "[email protected]",
"ok": "[email protected]",
"hello": "[email protected]",
"hi": "[email protected] ",
}`;
const validationResult = validateJSON(jsonDataToValidate);
if (validationResult.valid) {
console.log("JSON is valid.");
} else {
console.log("JSON is not valid.");
console.error(`Error message: ${validationResult.error}`);
console.error(`Error occurred at position: ${validationResult.position}`);
}