Skip to content

Converts the format of all keys in dynamic JSON sring to another format, such as case and underscore

License

Notifications You must be signed in to change notification settings

chengshicheng/json-key-convertor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-key-convertor

Go Go Report Card Go doc codecov

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:

$ go get -u github.com/chengshicheng/json-key-convertor

This will retrieve the library.

Example

General Key Convert

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

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"}}
}

About

Converts the format of all keys in dynamic JSON sring to another format, such as case and underscore

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages