https://github.com/Andrew-M-C/go.jsonvalue
Quick Solution with Unstructured JSON data
https://github.com/Andrew-M-C/go.jsonvalue
go golang json
Last synced: 12 months ago
JSON representation
Quick Solution with Unstructured JSON data
- Host: GitHub
- URL: https://github.com/Andrew-M-C/go.jsonvalue
- Owner: Andrew-M-C
- License: other
- Created: 2019-10-03T14:16:41.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-08-21T02:50:24.000Z (over 1 year ago)
- Last Synced: 2024-08-21T03:25:49.869Z (over 1 year ago)
- Topics: go, golang, json
- Language: Go
- Homepage:
- Size: 553 KB
- Stars: 183
- Watchers: 6
- Forks: 19
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - Andrew-M-C/go.jsonvalue
- awesome-go - jsonvalue - A fast and convenient library for unstructured JSON data, replacing `encoding/json`. (JSON / Search and Analytic Databases)
- awesome-go-with-stars - jsonvalue - 10-10 | (JSON / Search and Analytic Databases)
- awesome-go-cn - jsonvalue - M-C/go.jsonvalue) [![包含中文文档][CN]](https://github.com/Andrew-M-C/go.jsonvalue) (JSON / 检索及分析资料库)
- awesome-go - jsonvalue - A fast and convenient library for unstructured JSON data, replacing `encoding/json`. (JSON / Search and Analytic Databases)
- awesome-go-cn - jsonvalue - M-C/go.jsonvalue) [![包含中文文档][CN]](https://github.com/Andrew-M-C/go.jsonvalue) (JSON / 检索及分析资料库)
- awesome-go-plus - jsonvalue - A fast and convenient library for unstructured JSON data, replacing `encoding/json`.  (JSON / Search and Analytic Databases)
- awesome-go - jsonvalue - A fast and convenient library for unstructured JSON data, replacing `encoding/json`. (JSON / Search and Analytic Databases)
- awesome-go - jsonvalue - A fast and convenient library for unstructured JSON data, replacing `encoding/json`. (JSON / Search and Analytic Databases)
- fucking-awesome-go - jsonvalue - A fast and convenient library for unstructured JSON data, replacing `encoding/json`. (JSON / Search and Analytic Databases)
README
# Jsonvalue - A Fast and Convenient Alternation of Go map[string]interface{}
[](https://github.com/Andrew-M-C/go.jsonvalue/actions/workflows/go_test_general.yml)
[](https://codecov.io/gh/Andrew-M-C/go.jsonvalue)
[](https://goreportcard.com/report/github.com/Andrew-M-C/go.jsonvalue)
[](https://codebeat.co/projects/github-com-andrew-m-c-go-jsonvalue-master)
[](https://pkg.go.dev/github.com/Andrew-M-C/go.jsonvalue@v1.4.0)
[](https://github.com/Andrew-M-C/go.jsonvalue/tree/v1.4.0)
[](https://opensource.org/license/MIT)
- [Wiki](./docs/en/README.md)
- [中文版](./docs/zh-cn/README.md)
Package **jsonvalue** is for handling unstructured JSON data or customizing JSON marshaling. It is far more faster and convenient than using `interface{}` with `encoding/json`.
Please refer to [pkg site](https://pkg.go.dev/github.com/Andrew-M-C/go.jsonvalue) or [wiki](./docs/en/README.md) for detailed usage and examples.
Especially, please check for jsonvalue's [programming scenarios](./docs/en/10_scenarios.md).
## Import
Use following statements to import jsonvalue:
```go
import (
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
```
## Quick Start
Sometimes we want to create a complex JSON object like:
```json
{
"someObject": {
"someObject": {
"someObject": {
"message": "Hello, JSON!"
}
}
}
}
```
With `jsonvalue`, It is quite simple to implement this:
```go
v := jsonvalue.NewObject()
v.MustSet("Hello, JSON").At("someObject", "someObject", "someObject", "message")
fmt.Println(v.MustMarshalString())
// Output:
// {"someObject":{"someObject":{"someObject":{"message":"Hello, JSON!"}}}
```
Similarly, it is quite easy to create sub-arrays like:
```json
[
{
"someArray": [
"Hello, JSON!"
]
}
]
```
```go
v := jsonvalue.NewArray()
v.MustSet("Hello, JSON").At(0, "someObject", 0)
fmt.Println(v.MustMarshalString())
// Output:
// [{"someObject":["Hello, JSON"]}]
```
In opposite, to parse and read the first JSON above, you can use jsonvalue like this:
```go
const raw = `{"someObject": {"someObject": {"someObject": {"message": "Hello, JSON!"}}}}`
s := jsonvalue.MustUnmarshalString(s).GetString("someObject", "someObject", "someObject", "message")
fmt.Println(s)
// Output:
// Hello, JSON!
```
However, it is quite complex and annoying in automatically creating array. I strongly suggest using `SetArray()` to create the array first, then use `Append()` or `Insert()` to set array elements. Please refer go [godoc](https://godoc.org/github.com/Andrew-M-C/go.jsonvalue) or [Wiki](./docs/en/README.md).