I found not much resources on Dynamic JSON Parsing. Even if we use JSON.Unmarshall, it is a pain to access the elements of the interface(if you unmarshall into an empty interface). Moreover, Structs cant be used for Dynamic Parsing as we have no idea what the JSON file will look like. So, I wrote a digger in which we easily query the JSON interface not having to use reflect. This supports Nested JSON and Arrays. This is as long as the JSON file has no empty lines, the arrays are of string type and written in one line and integers are not supported yet. [A limitation which can be improved!]
- This program takes JSON file to read in form of User input. It Provides a low level JSON Unmarshall and works on files Specifically formatted in a way currently. I am open to anyone who wants to contribute for this.
- The Program Reads the JSON file line by line and appends the objects into map[string]interface{}. This is done using our own logic and I have NOT used json.Unmarshall for this.
- The Next Step is it lists out the parsed JSON in an indented fashion to give user an idea of the JSON file.
- Next, the User is asked to enter a query and if the query is correct, the value is fetched and shown.
Build a CLI Tool to Parse and Query Dynamic JSON in Golang https://betterprogramming.pub/build-a-cli-tool-to-parse-and-query-dynamic-json-in-golang-cc1db72b1e85
Usage: >>> jsondigger >>> ./check.json
- This program should take a text file path as an argument and generate a JSON file using the same data or an error if the text file has incorrect json format.
- All the text data is of string type.
- We will be able to generate a “well-formatted” (indented) JSON file.
- We will be using stacks for validation and some string validation
- This program must have a “help” option, to know how to use this tool.
https://betterprogramming.pub/build-your-first-golang-cli-tool-for-json-validation-30dd4ff0b963
Usage: >>> jsonvalidator
Options:
- pretty
1 for Generating pretty JSON file and 0 for only validation - verbose
0 for no error description and 1 for error description
- Clone this Project.
- Make Sure Golang and Make are Installed and Open the root directory of the project.
- Run
$ make install
in the CLI - Run
$ make digger
- Run
$ make validator
[If you want to run jsonvalidator]