An open API service indexing awesome lists of open source software.

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.

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


  • Open your terminal and execute:


  • npm install -g json-verifier

  • After installing, open your terminal and run 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


  • Install the package as above, then use as mentioned below


  • For verifying and parsing a json object string

    ```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}
    ```

  • For verifying and parsing JSON from file and folder path

    ```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

  • Clone the repo and install the dependencies by running the following command:


  • npm install

  • Execute the command to run the test files present in the repository


  • npm test

  • Note: You can remove/add any test files you want and just change the directories array in index.test.js file if deleting/adding any new folder.
  • Contribution and Issue reporting


  • Please fork the repository and raise a pull request for the enhancement/bug-fix/feature/documentation.

  • For issue reporting and feature requests, please create a new issue with proper details of the requirements/steps to reproduce and proper screenshots/videos/logs, etc to help me resolve it quickly and properly. Use proper tags to help me sort and filter all the issues and it will be easy for me as well.

    Thanks for using my project and reading till here! Any help is much appreciated!!