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.

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
πŸŽ₯ Audio/video file upload with real-time preview
πŸ—£οΈ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
πŸ“€ Export interview's subtitles in VTT format

Read full post

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay