Originally intended to be used with json.Unmarshal, this is a golang library used to able get and set jsonpaths (even nonexistent paths).
$ go get github.com/mdaverde/jsonpathsample:=`{ "owner": { "name": "john doe", "contact": { "phone": "555-555-5555" } } }`varpayloadinterface{}
err:=json.Unmarshal([]byte(sample), &payload)
must(err)
err=jsonpath.Set(&payload, "owner.contact.phone", "333-333-3333")
must(err)
value, err:=jsonpath.Get(payload, "owner.contact.phone")
must(err)
// value == "333-333-3333"Returns the value at that json path as interface{} and if an error occurred
Sets value on data at that json path
Note: you'll want to pass in a pointer to data so that the side effect actually is usable
This is type of error returned with using jsonpath.Get on a nonexistent path:
value, err:=Get(data, "where.is.this")
if_, ok:=err.(DoesNotExist); !ok&&err!=nil {
// other error
}$ go test.MIT © mdaverde