DEV Community

eco9999
eco9999

Posted on

1 2

Golang New JSON parser/interpreter

Hi gophers and other fellow life forms.

About 6 months ago we have started a new project that font-end side with JS/Node.js/React and back-end side with Golang. I was working for the back-end side everything was O.K. But the project grows the 'encoding/json' package get inefficient. For example we are not able to change a 'key' or change any value with something else. Thats because we switch to some custom packages for manipulating JSON.

At this point. I figure out 'How about I wrote a packege for JSON manipulating'

And than I start working on it. But dealing with such a project was not easy.

First I had to find a way to validate my result. Manually writing unit tests was impossible for large data. Thats because I started to integrate Node.js to this project for test-case creation and validation. And it worked perfectly.

I use a continius integration platform for test automation. And wrote a detailed documentation on GoDoc.

And It has a cool gopher logo you should see :)

Well today its finally ready for public release.

Name of this package is JIN

It is super easy to use and much more important its super fast.
I just want to share with this to you guys.

Let's look at with an example.

    // this is the JSON we will work on
    data := []byte(`{"repo":{"name":"ecoshub/jin"},"others":["jin","dev.to"]}`)

we are going to try to access the 'dev.to' string in 'others' array.

    // In order to Unmarshal a JSON.
    // first we have to define those structs properly.
    type Repository struct {
        Name string         `json:"name"`
    }

    type Data struct {
        Repo   Repository   `json:"repo"`
        Others []string     `json:"others"`
    }
    // an empty data struct
    var newData Data

    // finally Unmarshaling.
    err := json.Unmarshal(data, &newData)

    // standard error implementation.
    if err != nil {
        fmt.Println("error:", err)
    }
    fmt.Prinln(newData.Others[1])

Or you can use this. with no struct defination.

    // just one line of code.
    value, err := jin.Get(data, "others", "1")

    // standard error implementation.
    if err != nil {
        fmt.Println("error:", err)
    }
    fmt.Println(string(value))

Do not miss-understand me I am not the first person who figure out this simple and elegant definition. I am just trying to expand and improve JSON manipulation.

This package has over 90 functions/methods for ease JSON manipulation, build and formating needs. Some useful functions that i like to mention.

-Add(), AddKeyValue(), Set(), SetKey() Delete(), Insert(), IterateArray(), IterateKeyValue() Tree().

There is a very detailed explanations and lots of examples in GoDoc .

Also I think you have to check out benchmark results :)

This is the link of repository:
https://github.com/ecoshub/jin

Please do not hesitate to fork/clone pull-request.

Thank you so much for your time.

Have a good day.

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 👀

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more