Skip to content

Jsonpath-reverse is a tool used for creating json reversed by expression like jsonpath for go.

Notifications You must be signed in to change notification settings

seeadoog/jsonpath-reverse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

josnref for go

usage

jsonpath-reverse is a tool used for creating jsonpath reversed. with this tool ,you can create a json string by specific query string like "$.user.name" and set a value like 'bob' to it. The tool will generate a json string such as follow:

{
   "user":{
       "name":"bob"
   }
}

get start

  • install
    go get -t github.com/seeadoog/jsonpath-reverse
  • use
import (
	"fmt"
	"encoding/json"
	jsonref "github.com/skyniu/jsonpath-reverse"
)
type User struct {
	Name string `json:"name"`
	Age int `json:"age"`
}

func main() {
	jp, err := Compile("a.name[0]")
	if err != nil {
		panic(err)
	}

	var a any
	err = jp.Set(&a, 1)
	if err != nil {
		panic(err)
	}
	ns, _ := json.MarshalIndent(a, "", "    ")
	fmt.Println(string(ns))
}

the output json is :

{
    "a": {
        "name": [
            1
        ]
    }
}

About

Jsonpath-reverse is a tool used for creating json reversed by expression like jsonpath for go.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages