https://github.com/shellyln/go-small-jsonpath
Small, feature limited JSONPath (+dialect) implementation.
https://github.com/shellyln/go-small-jsonpath
go golang golang-library json jsonpath parser
Last synced: about 2 months ago
JSON representation
Small, feature limited JSONPath (+dialect) implementation.
- Host: GitHub
- URL: https://github.com/shellyln/go-small-jsonpath
- Owner: shellyln
- License: mit
- Created: 2023-01-30T23:44:08.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-02-04T10:32:57.000Z (over 3 years ago)
- Last Synced: 2025-03-24T03:34:40.794Z (about 1 year ago)
- Topics: go, golang, golang-library, json, jsonpath, parser
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Go Small JSONPath
Small, feature limited JSONPath (+dialect) implementation.
[](https://github.com/shellyln/go-small-jsonpath/actions/workflows/test.yml)
[](https://github.com/shellyln/go-small-jsonpath/releases)
[](https://github.com/shellyln/go-small-jsonpath)
## ✅ Features
+ Query that returns a single value
+ Safe query; returns zero value on failure
+ Negative value index; index from the last element, e.g.. `foo[-1].bar`
## 🛑 Unsupported features
+ Query that returns multiple values
+ Conditional query
+ Wildcard
+ Descendant node query
+ Aggregate functions
+ Other functions
## ⭐ Dialect
### Function
#### **`first`**
Returns the first item in the array.
```js
$.foo.(first).bar
```
#### **`last`**
Returns the last item in the array.
```js
$.foo.(last).bar
```
#### **`length`**
Returns the length of the array.
```js
$.foo.(length)
```
## 🚀 Usage
```go
package main
import (
"fmt"
"github.com/shellyln/go-small-jsonpath/jsonpath"
)
func main() {
json, err := jsonpath.ReadString(`{"test":[{"abc":1},{"abc":10}]}`)
if err != nil {
fmt.Printf("ReadString: error = %v\n", err)
return
}
path, err := jsonpath.Compile(`$.test[1].abc`)
if err != nil {
fmt.Printf("Compile: error = %v\n", err)
return
}
v, err := path.Query(json) // returns nil, float64, string, []any, map[string]any
if err != nil {
fmt.Printf("Query: error = %v\n", err)
return
}
fmt.Printf("Query: %v\n", v) // float64(10)
vn := path.QueryAsNumberOrZero(json) // returns zero value on failure
fmt.Printf("QueryAsNumberOrZero: %v\n", vn) // float64(10)
vs := path.QueryAsStringOrZero(json) // returns zero value on failure
fmt.Printf("QueryAsStringOrZero: %v\n", vs) // "" (zero value)
}
```
## 🪄 Query examples
Data:
```json
{
"foo": [11, 12, 13],
"bar": "abcdefg",
"baz": -9876,
"qux": {
"quux": null
}
}
```
Queries:
```go
> $.foo[0]
11
> $['foo'][1]
12
> $['foo'][3]
error (index out of range)
> $['foo'][-1]
13
> $["foo"].(length)
3
> $["foo"].(first)
11
> $["foo"].(last)
13
> $.bar
"abcdefg"
> $.bar.(length)
error (unsupported function operand)
> $.qux.quux
nil
> $['qux'].quux
nil
> $['qux']["quux"]
nil
> $.foo
map[string]any{...}
> $.qux
[]any{...}
```
## ⚖️ License
MIT
Copyright (c) 2023 Shellyl_N and Authors.