Code Monkey home page Code Monkey logo

gojmapr's Introduction

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

gojmapr's People

Contributors

limiu82214 avatar pig858 avatar

Stargazers

Joseph Li avatar Andrew Burdyug avatar Clayton Kehoe avatar  avatar Dale Dude avatar  avatar  avatar Hank avatar  avatar Ignacio Chen avatar Eliah Rusin avatar peter avatar peter avatar Kerash avatar  avatar  avatar  avatar  avatar  avatar James avatar Fan Zhuang avatar  avatar

Watchers

 avatar

Forkers

pig858

gojmapr's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.