README.md (2943B)
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 7 go-jmespath is a GO implementation of JMESPath, 8 which is a query language for JSON. It will take a JSON 9 document and transform it into another JSON document 10 through a JMESPath expression. 11 12 Using go-jmespath is really easy. There's a single function 13 you 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) 23 result = 2 24 ``` 25 26 In the example we gave the ``search`` function input data of 27 `{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}` as well as the JMESPath 28 expression `foo.bar.baz[2]`, and the `search` function evaluated 29 the expression against the input data to produce the result ``2``. 30 31 The JMESPath language can do a lot more than select an element 32 from 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) 39 result = { "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) 47 result [ '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`]") 56 result = [ { age: 35 }, { age: 40 } ] 57 ``` 58 59 You can also pre-compile your query. This is usefull if 60 you 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 76 The example above only show a small amount of what 77 a JMESPath expression can do. If you want to take a 78 tour of the language, the *best* place to go is the 79 [JMESPath Tutorial](http://jmespath.org/tutorial.html). 80 81 One of the best things about JMESPath is that it is 82 implemented in many different programming languages including 83 python, ruby, php, lua, etc. To see a complete list of libraries, 84 check out the [JMESPath libraries page](http://jmespath.org/libraries.html). 85 86 And finally, the full JMESPath specification can be found 87 on the [JMESPath site](http://jmespath.org/specification.html).