The Json editor is an easy-to-use json query and editing tool that you can use in command line and golang projects.
curl -Lo je https://sycki.com/f/bin/je // for linux
curl -Lo je https://sycki.com/f/bin/je-darwin // for mac
chmod +x je
mv je /usr/local/bin/je // move to your PATH
- Get string
echo '{"k1":"v1","k2":2,"k3":[{"k31":"v31"},{"k32":"v32"},{"k33":"v33"}]}' | je .k1 v1
- Get string
echo '{"k1":"v1","k2":2,"k3":[{"k31":"v31"},{"k32":"v32"},{"k33":"v33"}]}' | je .k2 2
- Get length string
echo '{"k1":"v1","k2":2,"k3":[{"k31":"v31"},{"k32":"v32"},{"k33":"v33"}]}' | je .k3.# 3
- Get string
echo '{"k1":"v1","k2":2,"k3":[{"k31":"v31"},{"k32":"v32"},{"k33":"v33"}]}' | je .k3.0.k31 v31
- Set struct
echo '{"k1":{"k11":"v11","k12":"v12"},"k2":"v2"}' | je .k2 '{"k21":"v21"}' {"k1":{"k11":"v11","k12":"v12"},"k2":{"k21":"v21"}}
- Set array
echo '{"k1":{"k11":"v11","k12":"v12"},"k2":"v2"}' | je .k2 '[{"k21":"v21"}]' {"k1":{"k11":"v11","k12":"v12"},"k2":[{"k21":"v21"}]}
- Set int
echo '{"k1":{"k11":"v11","k12":"v12"},"k2":"v2"}' | je .k2 '5' {"k1":{"k11":"v11","k12":"v12"},"k2":5}
- Set string
echo '{"k1":{"k11":"v11","k12":"v12"},"k2":"v2"}' | je .k2 '"5"' {"k1":{"k11":"v11","k12":"v12"},"k2":"5"}
- Set string
echo '{"k1":{"k11":"v11","k12":"v12"},"k2":"v2"}' | je .k2 \"5\" {"k1":{"k11":"v11","k12":"v12"},"k2":"5"}
- Set string
echo '{"k1":{"k11":"v11","k12":"v12"},"k2":"v2"}' | je .k2 v0 {"k1":{"k11":"v11","k12":"v12"},"k2":"v0"}
More demo see je_test.go
import github.com/sycki/je
func main() {
str := `{"k1":"v1","k2":2,k3":[{"k31":"v31"},{"k32":"v32"},{"k33":"v33"}]}`
r := je.Get(str, ".k3.0.k31")
println(r) // v31
}
import github.com/sycki/je
func main() {
str := `{"k1":"v1","k2":2,k3":[{"k31":"v31"},{"k32":"v32"},{"k33":"v33"}]}`
r := je.Set(str, ".k3", 3)
println(r) // {"k1":"v1","k2":2,k3":3}
}