Giter Club home page Giter Club logo

jsonpath's Introduction

JSONPath

JSONPath support for Ruby.

For more information on JSONPath, see [Stefan Goessner's blog entry] 1, or the [JS and PHP implementations] 2 on Google Code.

Installing

gem install bruce-jsonpath --source 'http://gems.github.com'

Dependencies

JSONPath uses [Treetop] 3 for parsing.

Completeness

As of 2009-07-17, this implementation passes all tests from the [JS and PHP implementations] 2 (after modifying the script expressions for Ruby) -- in addition to its own expanded test suite.

Usage

Execute JSONPath queries against a Ruby data structure (as would be parsed from JSON using the json or yajl gems).

Only one method is needed:

JSONPath.lookup(hash_or_array, path)

Features

It supports hash traversal by key:

JSONPath.lookup({"a" => 1}, '$.a')
# => [1]
JSONPath.lookup({"foo" => {"bar baz" => 2}}, "$.foo['bar baz']")
# => [2]

Array traversal by index, including start:stop:step slices:

JSONPath.lookup([1, 2, [3, 4, 5], 6], '$[2][-2:]')
# => [4, 5]

Wildcards:

JSONPath.lookup({"a" => {"b" => 3, "c" => 2}}, "$.a.*")
# => [3, 2]

Descendant traversal (think // in XPath):

JSONPath.lookup({'e' => 1, 'b' => [{'e' => 3}]}, '$..e')
# => [1, 3]

Peek at the tests for more ideas.

Experimental Support

It has experimental support for JSONPath's script expressions, including filters. Since JSONPath uses the underlying language in script expressions, that means we have access to Ruby (supporting arbitrarily complex traversal). As in other JSONPath implementations, @ is replaced by the current node.

lists = [
  [1, 2, 3, 4],
  [5, 6],
  [7, 8, 9, 10]
]
JSONPath.lookup(lists, "$.*[(@.length - 1)]")
=> [4, 6, 10]

And filters:

books = [
  {"name" => 'Bruce', "age" => 29},
  {"name" => "Braedyn", "age" => 3},
  {"name" => "Jamis", "age" => 2},
]
JSONPath.lookup(people, "$[?(@['age'] % 2 == 0)].name")
# => ['Jamis']

For more information, see the the [JSONPath introductory article] 1.

Contributing and Reporting Issues

The [project] 4 is hosted on [GitHub] 5, where I gladly accept pull requests.

If you run into any problems, please either (in order of preference) post something on the [issue tracker] 6, send me a message on GitHub, or email me.

Copyright

Copyright (c) 2009 Bruce Williams, based on work by Stefan Goessner. See LICENSE.

jsonpath's People

Stargazers

Angus H. avatar Demetrius Johnson avatar  avatar Li Wei avatar  avatar Bret Young avatar Gabriele Renzi avatar  avatar Thomas Hanley avatar Lincoln Ritter avatar Ilya Grigorik avatar David Lee avatar Joshua Hull avatar Matt Patterson avatar Tim Cowlishaw avatar Stephane Bellity avatar Eric Lindvall avatar Bruce Williams avatar

Watchers

Bruce Williams avatar James Cloos avatar  avatar  avatar

jsonpath's Issues

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.