modulepackage
0.0.0-20201008003839-dcb0e085768f
Repository: https://github.com/elliotcourant/jsonpath.git
Documentation: pkg.go.dev
# README
jsonpath
Go implementation of jsonpath
This is still a work in progress, updates to come.
Example
const jsonString = `{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
},
{
"type" : "mobile",
"number": "0913-8532-8492"
}
]
}`
result, err := Jsonpath([]byte(jsonString), "$.phoneNumbers[*].type")
if err != nil {
log.Fatal(err)
}
for _, item := range result {
fmt.Println(item)
}
// Output:
// iPhone
// home
// mobile
Supported operations
There are still a few operations which this library does not support but the goal is to support all of these path operations.
Operation | Supported | Description |
---|---|---|
$ | Yes | The root object/element. |
@ | No | The current object/element. (To be added). |
. or [] | Yes | Child operator. Within [] single quotes or double quotes can be used. |
.. | Yes | Recursive decent. |
* | Yes | Wildcard. All objects/elements regardless of their names. |
[] | Yes | subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator. |
[,] | Yes | Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set. |
[start:end:step] | No | Array slice operator borrowed from ES4. (To be added). |
?() | No | Applies a filter (script) expression. (To be added). |
() | No | Script expression, using the underlying script engine. (To be added). |
# Functions
Jsonpath will evaluate the provided jsonpath on the provided json.
NewEvaluator will compile the provided jsonpath and create an object that can run that expression on provided json objects.