Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bserdar/gojsonpath
Go JSON Path implementation with a flexible document model
https://github.com/bserdar/gojsonpath
go json jsonpath
Last synced: about 1 month ago
JSON representation
Go JSON Path implementation with a flexible document model
- Host: GitHub
- URL: https://github.com/bserdar/gojsonpath
- Owner: bserdar
- License: apache-2.0
- Created: 2024-03-28T05:59:58.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-04-12T23:21:39.000Z (9 months ago)
- Last Synced: 2024-06-19T16:29:29.213Z (7 months ago)
- Topics: go, json, jsonpath
- Language: Go
- Homepage:
- Size: 194 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![GoDoc](https://godoc.org/github.com/bserdar/gojsonpath?status.svg)](https://godoc.org/github.com/bserdar/gojsonpath)
[![Go Report Card](https://goreportcard.com/badge/github.com/bserdar/gojsonpath)](https://goreportcard.com/report/github.com/bserdar/gojsonpath)
[![Build Status](https://github.com/bserdar/gojsonpath/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/bserdar/gojsonpath/actions/workflows/CI.yml)# JSONPath
This is a Go implementation of JSONPath introduced in:
https://goessner.net/articles/JsonPath/
as well as JSON path expressions of the form:
```
/key1/index/*/key2
```with the following exceptions:
* If a path component is a it should be a valid identifier or
number. If that is not the case, it should be a string literal,
that is, quoted.For example: `$.dash-key` is invalid. You must quote it:
`$."dash-key"` or `$.'dash-key'`.* Function and method calls must have a possibly empty argument
list.For example: `length(@)` and `@.length()` are valid, but
`@.length` will look for a `length` key under the current node.* Decimals cannot start with a period, that is `.5` is
invalid. Use `0.5` instead. Decimal starting with a period
confuses the lexer.
* Regular expressions are not supported yet.## Using JSONPath
First, compile the path:
```
path, err:= gojsonpath.Parse("$.store.book[*].author")
```Then you can use:
```
results, err:= gojsonpath.Find(doc,path)
```or you can capture the full-path for the nodes that match:
``` go
result:=make([]any,0)
err := gojsonpath.Search(doc,path,func(elem []gojsonpath.Element) {
result=append(result,elem[len(elem)-1].Node)
})
```You can use the simple path parser to compile '/' separated paths:
```
path, err := gojsonpath.Parse("/store/book/*/author")
```Simple paths must start with '/', and may contain object keys, array
indexes, or '*' to match anything, separated by '/'.## Document model
This implementation of JSONPath works with a flexible document
model. To use the familiar `map[string]any` implementation of JSON,
use:``` go
var jsonDoc any
json.Unmarshal(jsonBytes,&jsonDoc)
doc := gojsonpath.MapModel{Doc:jsonDoc}
```The `DocModel` interface provides a view of the underlying model, so
the JSON document does not have to be a `map[string]any`. Any
hierarchical document model supporting key-value, array, and value
nodes can be used.