/jsons

go json extension, support the database type.

Primary LanguageGoMIT LicenseMIT

jsons- golang library

Golang like scripting languages using the json library.

Download and Install

go get github.com/zooyer/jsons

Features

  • type: Raw/Bool/Number/String/Array/Object/Value.
  • compatible with standard json library.
  • support orm model mapping.
  • chain calls.

Example

package main

import (
	"fmt"
	
	"github.com/zooyer/jsons"
)

func main() {
	var json = `
		{
			"a": {
				"b": {
					"c": {
						"name": "test"
					}
				}
			}
		}
	`
	value, err := jsons.Unmarshal([]byte(json))
	if err != nil {
		panic(err)
	}

	c := value.Get("a", "b", "c")
	fmt.Println("c:", c.JSONString())

	name := c.Get("name")
	fmt.Println("name:", name.String())

	c.Set("name", "abc")
	fmt.Println("json:", value.JSONString())

	json = `
		[
			{
				"name": "z1",
				"age": 15
			},
			{
				"name": "z2",
				"age": 13
			},
			{
				"name": "z3",
				"age": 19
			},
			{
				"name": "z4",
				"age": 14
			}
		]
	`

	value, err = jsons.Unmarshal([]byte(json))
	if err != nil {
		panic(err)
	}

	value.Sort(func(i, j int) bool {
		return value.Int(i, "age") < value.Int(j, "age")
	})
	fmt.Println("sort:", value.JSONString())
	value.Reverse()
	fmt.Println("reverse:", value.JSONString())
}

ouput:

c: {"name":"test"}
name: test
json: {"a":{"b":{"c":{"name":"abc"}}}}
sort: [{"age":13,"name":"z2"},{"age":14,"name":"z4"},{"age":15,"name":"z1"},{"age":19,"name":"z3"}]
reverse: [{"age":19,"name":"z3"},{"age":15,"name":"z1"},{"age":14,"name":"z4"},{"age":13,"name":"z2"}]