Coder Social home page Coder Social logo

jmespath.rb's Introduction

jmespath.rb

Gitter chat Build Status

An implementation of JMESPath for Ruby. This implementation supports searching JSON documents as well as native Ruby data structures.

Installation

$ gem install jmespath

Basic Usage

Call JMESPath.search with a valid JMESPath search expression and data to search. It will return the extracted values.

require 'jmespath'

JMESPath.search('foo.bar', { foo: { bar: { baz: "value" }}})
#=> {baz: "value"}

In addition to accessing nested values, you can exact values from arrays.

JMESPath.search('foo.bar[0]', { foo: { bar: ["one", "two"] }})
#=> "one"

JMESPath.search('foo.bar[-1]', { foo: { bar: ["one", "two"] }})
#=> "two"

JMESPath.search('foo[*].name', {foo: [{name: "one"}, {name: "two"}]})
#=> ["one", "two"]

If you search for keys no present in the data, then nil is returned.

JMESPath.search('foo.bar', { abc: "mno" })
#=> nil

See the JMESPath specification for a full list of supported search expressions.

Indifferent Access

The examples above show JMESPath expressions used to search over hashes with symbolized keys. You can use search also for hashes with string keys or Struct objects.

JMESPath.search('foo.bar', { "foo" => { "bar" => "value" }})
#=> "value"

data = Struct.new(:foo).new(
  Struct.new(:bar).new("value")
)
JMESPath.search('foo.bar', data)
#=> "value"

JSON Documents

If you have JSON documents on disk, or IO objects that contain JSON documents, you can pass them as the data argument.

JMESPath.search(expression, Pathname.new('/path/to/data.json'))

File.open('/path/to/data.json', 'r', encoding:'UTF-8') do |file|
  JMESPath.search(expression, file)
end

Links of Interest

License

This library is distributed under the apache license, version 2.0

Copyright 2014 Trevor Rowe; All rights reserved.

Licensed under the apache license, version 2.0 (the "license"); You may not use this library except in compliance with the license. You may obtain a copy of the license at:

http://www.apache.org/licenses/license-2.0

Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "as is" basis, without warranties or conditions of any kind, either express or implied.

See the license for the specific language governing permissions and limitations under the license.

jmespath.rb's People

Contributors

alextwoods avatar chrisroberts avatar geophilusd avatar gitter-badger avatar grddev avatar iconara avatar jamesls avatar johnallen3d avatar kddnewton avatar mtichner avatar mullermp avatar mvastola avatar notethan avatar pmorton avatar pocke avatar sferik avatar stenlarsson avatar timcraft avatar trevorrowe avatar valtri 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.