Installations
npm install parse-json
Developer
sindresorhus
Developer Guide
Module System
ESM
Min. Node Version
>=18
Typescript Support
Yes
Node Version
18.18.2
NPM Version
9.2.0
Statistics
347 Stars
61 Commits
35 Forks
11 Watching
1 Branches
15 Contributors
Updated on 19 Nov 2024
Languages
JavaScript (79.91%)
TypeScript (20.09%)
Total Downloads
Cumulative downloads
Total Downloads
13,045,330,754
Last day
-3.9%
13,386,029
Compared to previous day
Last week
3.1%
73,177,135
Compared to previous week
Last month
21%
291,956,609
Compared to previous month
Last year
3.8%
2,933,588,946
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
parse-json
Parse JSON with more helpful errors
Install
1npm install parse-json
Usage
1import parseJson, {JSONError} from 'parse-json'; 2 3const json = '{\n\t"foo": true,\n}'; 4 5 6JSON.parse(json); 7/* 8undefined:3 9} 10^ 11SyntaxError: Unexpected token } 12*/ 13 14 15parseJson(json); 16/* 17JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' 18 19 1 | { 20 2 | "foo": true, 21> 3 | } 22 | ^ 23*/ 24 25 26parseJson(json, 'foo.json'); 27/* 28JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json 29 30 1 | { 31 2 | "foo": true, 32> 3 | } 33 | ^ 34*/ 35 36 37// You can also add the filename at a later point 38try { 39 parseJson(json); 40} catch (error) { 41 if (error instanceof JSONError) { 42 error.fileName = 'foo.json'; 43 } 44 45 throw error; 46} 47/* 48JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json 49 50 1 | { 51 2 | "foo": true, 52> 3 | } 53 | ^ 54*/
API
parseJson(string, reviver?, filename?)
Throws a JSONError
when there is a parsing error.
string
Type: string
reviver
Type: Function
Prescribes how the value originally produced by parsing is transformed, before being returned. See JSON.parse
docs for more.
filename
Type: string
The filename displayed in the error message.
JSONError
Exposed for instanceof
checking.
fileName
Type: string
The filename displayed in the error message.
codeFrame
Type: string
The printable section of the JSON which produces the error.
rawCodeFrame
Type: string
The raw version of codeFrame
without colors.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
- Info: security policy file detected: .github/security.md:1
- Info: Found linked content: .github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: .github/security.md:1
- Info: Found text in security policy: .github/security.md:1
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
Found 14/30 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/parse-json/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/parse-json/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:22
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 14 are checked with a SAST tool
Score
4.4
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More