https://github.com/chengshicheng/json-key-convertor
Converts the format of all keys in dynamic JSON sring to another format, such as case and underscore
https://github.com/chengshicheng/json-key-convertor
camelcase dynamic golang json jsonstring key-converter snakecase
Last synced: 5 months ago
JSON representation
Converts the format of all keys in dynamic JSON sring to another format, such as case and underscore
- Host: GitHub
- URL: https://github.com/chengshicheng/json-key-convertor
- Owner: chengshicheng
- License: mit
- Created: 2023-04-16T14:06:32.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-22T11:19:48.000Z (about 3 years ago)
- Last Synced: 2024-01-29T11:10:45.516Z (over 2 years ago)
- Topics: camelcase, dynamic, golang, json, jsonstring, key-converter, snakecase
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-key-convertor
[](https://github.com/chengshicheng/json-key-convertor/actions/workflows/go.yml)
[](https://goreportcard.com/report/github.com/chengshicheng/json-key-convertor)
[](https://pkg.go.dev/github.com/chengshicheng/json-key-convertor)
[](https://codecov.io/gh/chengshicheng/json-key-convertor)
json-key-convertor is a Go package, it provides a simple way to converts the format of all keys in dynamic JSON to another format, such as case and underscore
# Getting Started
## Features
- Four built-in key conversion methods, lowercase, uppercase, snakecase, camlecase
- Support for nested multi-level json
- Custom key convert methods
## Installing
To start using json-key-convertor, install Go and run `go get`:
```sh
$ go get -u github.com/chengshicheng/json-key-convertor
```
This will retrieve the library.
## Example
### General Key Convert
```Go
package main
import (
convertor "github.com/chengshicheng/json-key-convertor"
)
const jsonStr = `{"name":{"first_name":"Janet","last_name":"Prichard"},"age":47}`
func main() {
value, err := convertor.ConvertKey([]byte(jsonStr), convertor.Camel)
if err != nil {
println(err.Error())
}
println(string(value))
// {"Age":47,"Name":{"FirstName":"Janet","LastName":"Prichard"}}
}
```
### Custom Key Convert
```Go
package main
import (
convertor "github.com/chengshicheng/json-key-convertor"
)
const jsonStr = `{"name":{"first_name":"Janet","last_name":"Prichard"},"age":47}`
func myPrefixFunc(s string) string {
return "my_" + s
}
func main() {
// register convert function
convertor.RegisterConvertFunc("myprefix", myPrefixFunc)
value, err := convertor.ConvertKey([]byte(jsonStr), "myprefix")
if err != nil {
println(err.Error())
}
println(string(value))
// {"my_age":47,"my_name":{"my_first_name":"Janet","my_last_name":"Prichard"}}
}
```