Skip to content
forked from joncalhoun/qson

Convert URL Query Params into JSON or Go Objects (via JSON)

License

Notifications You must be signed in to change notification settings

fastfishio/qson

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

qson

WARNING This isn't actively maintained, but it should work as a proof of concept for anyone who is interested in seeing how you might write a library like this for your own projects.

Convert URL query strings into JSON so that you can more easily parse them into structs/maps in Go.

I wrote this to help someone in the Gopher Slack, so it isn't really 100% complete but it should be stable enough to use in most production environments and work well as a starting point if you need something more custom.

If you end up using the package, feel free to submit any bugs and feature requests and I'll try to get to those updated time permitting.

Usage

You can either turn a URL query param into a JSON byte array, or unmarshal that directly into a Go object.

Transforming the URL query param into a JSON byte array:

import "github.com/joncalhoun/qson"

func main() {
  b, err := qson.ToJSON("bar%5Bone%5D%5Btwo%5D=2&bar[one][red]=112")
  if err != nil {
    panic(err)
  }
  fmt.Println(string(b))
  // Should output: {"bar":{"one":{"red":112,"two":2}}}
}

Or unmarshalling directly into a Go object using JSON struct tags:

import "github.com/joncalhoun/qson"

type unmarshalT struct {
	A string     `json:"a"`
	B unmarshalB `json:"b"`
}
type unmarshalB struct {
	C int `json:"c"`
}

func main() {
  var out unmarshalT
  query := "a=xyz&b[c]=456"
  err := Unmarshal(&out, query)
  if err != nil {
  	t.Error(err)
  }
  // out should equal
  //   unmarshalT{
	// 	  A: "xyz",
	// 	  B: unmarshalB{
	// 	  	C: 456,
	// 	  },
	//   }
}

To get a query string like in the two previous examples you can use the RawQuery field on the net/url.URL type.

About

Convert URL Query Params into JSON or Go Objects (via JSON)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%