https://github.com/viralcodex/json-verifier-cli
This is a custom JSON parser for validating and parsing .json files and objects through CLI or in-code.
https://github.com/viralcodex/json-verifier-cli
args cli cli-tool js json json-parser npm-package
Last synced: about 1 month ago
JSON representation
This is a custom JSON parser for validating and parsing .json files and objects through CLI or in-code.
- Host: GitHub
- URL: https://github.com/viralcodex/json-verifier-cli
- Owner: viralcodex
- Created: 2024-11-22T19:30:43.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2025-02-22T18:53:40.000Z (4 months ago)
- Last Synced: 2025-04-17T02:46:53.498Z (about 2 months ago)
- Topics: args, cli, cli-tool, js, json, json-parser, npm-package
- Language: JavaScript
- Homepage:
- Size: 4.44 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
Awesome Lists containing this project
README
JSON Verifier (with CLI tool and Parser)
This is a JSON verifier and parser created as a simple project from Coding challenges but now being developed as a full fledged project/package.
This tool can be used to verify and parse JSON files.
You can verify and parse a single file or a folder consisting of JSON files using the CLI tool or use the exported functions, in your own code (mentioned below).How to Use
For Running in CLI
npm install -g json-verifier
json-verifier
, you will get the options on how to use the tool as below.```markdown
Options:
-V, --version output the package version number
-f, --file verify the JSON file for the file path given
-fd, --folder verify all the JSON files and parse them for the folder path given
-d, --max-depth set the max depth for the JSON file(s) you want to check
-l --logging Choose if you want to log the results of the verification. By default it is enabled.
-h, --help display help for available options
```
For importing as a library in code
```javascript
import { verifyJson, verifyJsonFromPath } from 'json-verifier-cli';
/**
* for verifying and parsing a json object string directly from code
*/
const obj = `{"key":"value", "key2":true}`;
const maxDepth : number = 10 // (optional) any integer above 0 (default is 19)
console.log(verifyJson(obj, maxDepth)); //output: {key:"value", list:true}
```
```javascript
/**
* absoluteFilePath.json
* {
* "key":123,
* "key2": "value"
* }
*/
const filePath = './absoluteFilePath.json'
const maxDepth : number = 10 // (optional) any integer above 0 (default is 19)
const enableLoggingToFile : boolean = false // (optional) Y, y, N, n (default is Yes or enabled)
/** outputs an object with filename and result (error or parsed JSON data)
* { file: "file1.json", result: },
*/
console.log(verifyJsonFromPath(filePath, maxDepth, enableLoggingToFile))
/**
* absoluteFolderPath/
* - file1.json
* - file2.json
* - file3.json ... and so on
*/
const folderPath = './absoluteFolderPath'
/** outputs an array of object with filename and result (error or parsed JSON data)
* [
* { file: "file1.json", result: },
* { file: "file2.json", result: },
* ]
*/
console.log(verifyJsonFromPath(folderPath, maxDepth, enabledLoggingToFile))
```
Implementation
I would specially like to mention this repo by EliaSM which helped me craft an approach and write a version of mine. Please check their code out written in Typescript and try it out too!!(I'm still learning).
For CLI support, I used Commander.js and Chalk.
I use Javascript and go through character by character for the whole data and create tokens, then stop whenever there is an invalid character or pattern in the JSON data from the file.
This is similar to generator function used in the above repository and this approach helps in parsing large data by not parsing it all at once but one by one and stopping whenever there is an error or invalidity is found.
(Even though generator function is present in JS as well, I wrote a non-generator approach using OOPS to increase new)
Testing
I have used Jest for testing and test files are from Coding Challenges (folders step1 to step4) and from official JSON website (folder step5) which contains 40 standard tests for checking whether a JSON is valid or not.
For Already Present Test Files in the project
npm install
npm test
directories
array in index.test.js
file if deleting/adding any new folder.Contribution and Issue reporting
Thanks for using my project and reading till here! Any help is much appreciated!!