GitXplorerGitXplorer
m

jsonpath

public
24 stars
6 forks
6 issues

Commits

List of commits on branch main.
Unverified
b3fab9520682bc1f7814ced61f896a81d0554381

update badges

mmdaverde committed 3 years ago
Unverified
cde5f5152f8f6511e3833dc1d49b5a41d751bcea

follow go doc convention

mmdaverde committed 3 years ago
Unverified
6ecfe1d6486d385c5613de588d625db9257dbdbe

doc.go

mmdaverde committed 3 years ago
Unverified
842c08d0fe1ab4ee5a91c4cd0384476c437c6340

gets rid of ineffectual err assignments

mmdaverde committed 3 years ago
Unverified
c27d9c2d9ca341ecae6c0532c3f8ade798d98411

renames err to idiomatic go

mmdaverde committed 3 years ago
Unverified
6c5085d21fce11c1f7bac566365086708a198dfb

gofmt

mmdaverde committed 3 years ago

README

The README file for this repository.

jsonpath build Go Report Card GoDoc

Originally intended to be used with json.Unmarshal, this is a golang library used to able get and set jsonpaths (even nonexistent paths).

Install

$ go get github.com/mdaverde/jsonpath

Usage

sample := `{ "owner": { "name": "john doe", "contact": { "phone": "555-555-5555" } } }`

var payload interface{}

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"

API

jsonpath.Get(data interface{}, path string) (interface{}, error)

Returns the value at that json path as interface{} and if an error occurred

jsonpath.Set(data interface{}, path string, value interface{}) (error)

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

jsonpath.DoesNotExist error

Returned by jsonpath.Get on a nonexistent path:

value, err := Get(data, "where.is.this")
if _, ok := err.(DoesNotExist); !ok && err != nil {
    // other error
}

Testing

$ go test .

License

MIT © mdaverde