Giter Club home page Giter Club logo

gojsonpath's Introduction

GoDoc Go Report Card Build Status

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:

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:

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.

gojsonpath's People

Contributors

bserdar avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.