1# go-jmespath - A JMESPath implementation in Go 2 3[![Build Status](https://img.shields.io/travis/jmespath/go-jmespath.svg)](https://travis-ci.org/jmespath/go-jmespath) 4 5 6 7go-jmespath is a GO implementation of JMESPath, 8which is a query language for JSON. It will take a JSON 9document and transform it into another JSON document 10through a JMESPath expression. 11 12Using go-jmespath is really easy. There's a single function 13you use, `jmespath.search`: 14 15 16```go 17> import "github.com/jmespath/go-jmespath" 18> 19> var jsondata = []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}`) // your data 20> var data interface{} 21> err := json.Unmarshal(jsondata, &data) 22> result, err := jmespath.Search("foo.bar.baz[2]", data) 23result = 2 24``` 25 26In the example we gave the ``search`` function input data of 27`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}` as well as the JMESPath 28expression `foo.bar.baz[2]`, and the `search` function evaluated 29the expression against the input data to produce the result ``2``. 30 31The JMESPath language can do a lot more than select an element 32from a list. Here are a few more examples: 33 34```go 35> var jsondata = []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}`) // your data 36> var data interface{} 37> err := json.Unmarshal(jsondata, &data) 38> result, err := jmespath.search("foo.bar", data) 39result = { "baz": [ 0, 1, 2, 3, 4 ] } 40 41 42> var jsondata = []byte(`{"foo": [{"first": "a", "last": "b"}, 43 {"first": "c", "last": "d"}]}`) // your data 44> var data interface{} 45> err := json.Unmarshal(jsondata, &data) 46> result, err := jmespath.search({"foo[*].first", data) 47result [ 'a', 'c' ] 48 49 50> var jsondata = []byte(`{"foo": [{"age": 20}, {"age": 25}, 51 {"age": 30}, {"age": 35}, 52 {"age": 40}]}`) // your data 53> var data interface{} 54> err := json.Unmarshal(jsondata, &data) 55> result, err := jmespath.search("foo[?age > `30`]") 56result = [ { age: 35 }, { age: 40 } ] 57``` 58 59You can also pre-compile your query. This is usefull if 60you are going to run multiple searches with it: 61 62```go 63 > var jsondata = []byte(`{"foo": "bar"}`) 64 > var data interface{} 65 > err := json.Unmarshal(jsondata, &data) 66 > precompiled, err := Compile("foo") 67 > if err != nil{ 68 > // ... handle the error 69 > } 70 > result, err := precompiled.Search(data) 71 result = "bar" 72``` 73 74## More Resources 75 76The example above only show a small amount of what 77a JMESPath expression can do. If you want to take a 78tour of the language, the *best* place to go is the 79[JMESPath Tutorial](http://jmespath.org/tutorial.html). 80 81One of the best things about JMESPath is that it is 82implemented in many different programming languages including 83python, ruby, php, lua, etc. To see a complete list of libraries, 84check out the [JMESPath libraries page](http://jmespath.org/libraries.html). 85 86And finally, the full JMESPath specification can be found 87on the [JMESPath site](http://jmespath.org/specification.html). 88