Giter VIP home page Giter VIP logo

jsonpath's Introduction

Build Status

This was mostly an experiment to learn go and test using closures to interpret a JSON path. You should use https://github.com/PaesslerAG/jsonpath instead.

jsonpath

a (partial) implementation in Go based on Stefan Goener JSON Path

Limitations

  • No support for subexpressions : $books[(@.length-1)]
  • No support for filters : $books[?(@.price > 10)]
  • Strings in brackets must use double quotes : $["bookstore"]
  • Cannot operate on struct fields

The third limitation comes from using the text/scanner package from the standard library. The last one could be overcome by using reflection.

JsonPath quick intro

All expressions start $.

Examples (supported by the current implementation) :

  • $ the current object (or array)
  • $.books access to the key of an object (or $["books"]with bracket syntax)
  • $.books[1] access to the index of an array
  • $.books[1].authors[1].name chaining of keys and index
  • $["books"][1]["authors"][1]["name"] the same with braket syntax
  • $.books[0,1,3] union on an array
  • $["books", "songs", "movies"] union on an object
  • $books[1:3] second and third items of an array
  • $books[:-2:2] every two items except the last two of an array
  • $books[::-1] all items in reversed order
  • $..authors all authors (recursive search)

Checkout the tests for more examples.

Install

go get github.com/yalp/jsonpath

Usage

A jsonpath applies to any JSON decoded data using interface{} when decoded with encoding/json :

var bookstore interface{}
err := json.Unmarshal(data, &bookstore)
authors, err := jsonpath.Read(bookstore, "$..authors")

A jsonpath expression can be prepared to be reused multiple times :

allAuthors, err = jsonpath.Prepare("$..authors")
...
var bookstore interface{}
err := json.Unmarshal(data, &bookstore)
authors, err := allAuthors(bookstore)

The type of the values returned by the Read method or Prepare functions depends on the jsonpath expression.

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.