ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - limiu82214/gojmapr: get simple struct from complex json
get simple struct from complex json. Contribute to limiu82214/gojmapr development by creating an account on GitHub.
Visit Site

GitHub - limiu82214/gojmapr: get simple struct from complex json

GitHub - limiu82214/gojmapr: get simple struct from complex json

gojmapr

GitHub tag (latest by date) Go Reference License: MIT codecov Go Report Card github actions workflow Mentioned in Awesome Go

中文版文檔

gojmapr is a Golang library that allows for quick extraction of specified properties from complex JSON strings and converts them into corresponding Go structures.

With gojmapr, you don't need to declare a complete Go structure that corresponds to the entire JSON structure, just provide the required properties.

This makes gojmapr particularly useful for extracting specific data when accessing third-party resources, making your code more concise and readable.

Features

Easy to use: Easily extract required properties from JSON strings by adding a few tags. Supports nested properties: Easily extract required properties from JSON strings with multiple nested levels.

Installation

To use gojmapr, first add it to your Golang project:

go get github.com/limiu82214/gojmapr

Usage

To use the gojmapr library, simply import it into your code and follow these steps:

Define a struct that corresponds to the JSON string you want to parse. Add the gojmapr tag to each property in the struct to specify the path to extract the property from the JSON string (reference jpath). Use the gojmapr.Unmarshal function to parse the JSON string into a struct object.

Example

Here's a simple example that shows how to use the gojmapr library to extract properties from a JSON string.

package main

import (
    "fmt"

    "github.com/limiu82214/gojmapr"
)

func main() {
    jsonString := `{
        "user": {
            "name": "John",
            "email": "[email protected]"
        },
        "cart": {
            "items": [
                {
                    "product": {
                        "id": "123",
                        "name": "Product A",
                        "description": "Product A description",
                        "price": 10.99
                    },
                    "quantity": 2
                },
                {
                    "product": {
                        "id": "456",
                        "name": "Product B",
                        "description": "Product B description",
                        "price": 5.99
                    },
                    "quantity": 1
                }
            ],
            "total": 27.97
        },
        "shipping": {
            "method": "standard",
            "address": {
                "street": "123 Main St",
                "city": "Anytown",
                "state": "CA",
                "zip": "12345"
            },
            "fee": 5.99
        },
        "create_at": "2020-01-01T00:00:00Z"
    }`

    type tmpStruct struct {
        Name string `gojmapr:"user.name"`
    }

    var s tmpStruct
    err := gojmapr.Unmarshal([]byte(jsonString), &s)
    if err != nil {
        panic(err)
    }

    fmt.Println(s.Name) // Output: John

    type tmpStruct2 struct {
        ID    string  `gojmapr:"$.cart.items[0].product.id"`
        Price float64 `gojmapr:"$.cart.items.0.product.price"`
    }

    var s2 tmpStruct
    err := Unmarshal([]byte(jsonString), &s2)
    if err != nil {
        panic(err)
    }

    fmt.Println(s2.ID) // Output: 123
    fmt.Println(s2.Price) // Output: 10.99
}

More examples of usage can be found in the test code in the project.

Use other Unmarshal package


import jsoniter "github.com/json-iterator/go"

type tmpStruct struct {
    RequestID string `gojmapr:"$.request_id"`
}

SetUnmarshalFunc(jsoniter.Unmarshal) // You can use other Unmarshal module ex: json-iterator

var s tmpStruct
err := Unmarshal([]byte(jsonString), &s)
ex.Assert().Nil(err)
ex.Assert().Equal(ex.anserStruct.RequestID, s.RequestID)

gojmapr can use other Unmarshal package ex: json-iterator.

Testing

gojmapr uses the testify package for testing.
To run the tests, use the following command:

go test -v ./...

Dependency

Other

If you encounter any issues during use, please feel free to raise an issue on the GitHub project or contact me via email.
If you find this project helpful, please consider giving it a star.

LICENSE

MIT License

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory