jsonpath 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/README.md +48 -26
- data/Rakefile +5 -0
- data/lib/jsonpath.rb +1 -1
- data/lib/jsonpath/version.rb +1 -1
- data/test/test_jsonpath.rb +30 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c81ddcbb6e1a1224f9a068866f731a119a26806
|
4
|
+
data.tar.gz: 7147c3700e548741223faf65f81c570a8d3e1790
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3d4e55012e0ab3e1b969e89bb8ac3e4450178c0954710eaf06b214a4973a796186a1cd976f6fbc211723ca7061e74f0e600650f2252d77b87c8b40109fa5494
|
7
|
+
data.tar.gz: e993c5f494bc2910728d3da43ab3e0933808a5824c7d057c23ba0fa38ce04cd72fa76898058c5c48702ae8418ead5eb898b9dbd38d643f630bcc3423992aa881
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -22,7 +22,7 @@ There is stand-alone usage through the binary `jsonpath`
|
|
22
22
|
|
23
23
|
To use JsonPath as a library simply include and get goin'!
|
24
24
|
|
25
|
-
|
25
|
+
```ruby
|
26
26
|
require 'jsonpath'
|
27
27
|
|
28
28
|
json = <<-HERE_DOC
|
@@ -38,94 +38,116 @@ json = <<-HERE_DOC
|
|
38
38
|
}
|
39
39
|
}
|
40
40
|
HERE_DOC
|
41
|
-
|
41
|
+
```
|
42
42
|
|
43
43
|
Now that we have a JSON object, let's get all the prices present in the object. We create an object for the path
|
44
44
|
in the following way.
|
45
45
|
|
46
|
-
|
46
|
+
```ruby
|
47
47
|
path = JsonPath.new('$..price')
|
48
|
-
|
48
|
+
```
|
49
49
|
|
50
50
|
Now that we have a path, let's apply it to the object above.
|
51
51
|
|
52
|
-
|
52
|
+
```ruby
|
53
53
|
path.on(json)
|
54
54
|
# => [19.95, 8.95, 12.99, 8.99, 22.99]
|
55
|
-
|
55
|
+
```
|
56
56
|
|
57
57
|
Or on some other object ...
|
58
58
|
|
59
|
-
|
59
|
+
```ruby
|
60
60
|
path.on('{"books":[{"title":"A Tale of Two Somethings","price":18.88}]}')
|
61
61
|
# => [18.88]
|
62
|
-
|
62
|
+
```
|
63
63
|
|
64
64
|
You can also just combine this into one mega-call with the convenient `JsonPath.on` method.
|
65
65
|
|
66
|
-
|
66
|
+
```ruby
|
67
67
|
JsonPath.on(json, '$..author')
|
68
68
|
# => ["Nigel Rees", "Evelyn Waugh", "Herman Melville", "Tolkien"]
|
69
|
-
|
69
|
+
```
|
70
70
|
|
71
71
|
Of course the full JsonPath syntax is supported, such as array slices
|
72
72
|
|
73
|
-
|
73
|
+
```ruby
|
74
74
|
JsonPath.new('$..book[::2]').on(json)
|
75
75
|
# => [
|
76
76
|
# {"price"=>8.95, "category"=>"reference", "author"=>"Nigel Rees", "title"=>"Sayings of the Century"},
|
77
77
|
# {"price"=>8.99, "category"=>"fiction", "author"=>"Herman Melville", "title"=>"Moby Dick", "isbn"=>"0-553-21311-3"}
|
78
78
|
# ]
|
79
|
-
|
79
|
+
```
|
80
80
|
|
81
81
|
...and evals.
|
82
82
|
|
83
|
-
|
83
|
+
```ruby
|
84
84
|
JsonPath.new('$..price[?(@ < 10)]').on(json)
|
85
85
|
# => [8.95, 8.99]
|
86
|
-
|
86
|
+
```
|
87
87
|
|
88
88
|
There is a convenience method, `#first` that gives you the first element for a JSON object and path.
|
89
89
|
|
90
|
-
|
90
|
+
```ruby
|
91
91
|
JsonPath.new('$..color').first(object)
|
92
92
|
# => "red"
|
93
|
-
|
93
|
+
```
|
94
94
|
|
95
95
|
As well, we can directly create an `Enumerable` at any time using `#[]`.
|
96
96
|
|
97
|
-
|
97
|
+
```ruby
|
98
98
|
enum = JsonPath.new('$..color')[object]
|
99
99
|
# => #<JsonPath::Enumerable:...>
|
100
100
|
enum.first
|
101
101
|
# => "red"
|
102
102
|
enum.any?{ |c| c == 'red' }
|
103
103
|
# => true
|
104
|
-
|
104
|
+
```
|
105
105
|
|
106
106
|
You can optionally prevent eval from being called on sub-expressions by passing in :allow_eval => false to the constructor.
|
107
107
|
|
108
|
-
###
|
108
|
+
### More examples
|
109
109
|
|
110
110
|
For more usage examples and variations on paths, please visit the tests. There are some more complex ones as well.
|
111
111
|
|
112
|
+
### Conditional Operators Are Also Supported
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
def test_or_operator
|
116
|
+
assert_equal [@object['store']['book'][1], @object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] == 13 || @['price'] == 23)]").on(@object)
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_and_operator
|
120
|
+
assert_equal [], JsonPath.new("$..book[?(@['price'] == 13 && @['price'] == 23)]").on(@object)
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_and_operator_with_more_results
|
124
|
+
assert_equal [@object['store']['book'][1]], JsonPath.new("$..book[?(@['price'] < 23 && @['price'] > 9)]").on(@object)
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
### Running an individual test
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
ruby -Ilib:../lib test/test_jsonpath.rb --name test_wildcard_on_intermediary_element_v6
|
132
|
+
```
|
133
|
+
|
112
134
|
### Manipulation
|
113
135
|
|
114
136
|
If you'd like to do substitution in a json object, you can use `#gsub` or `#gsub!` to modify the object in place.
|
115
137
|
|
116
|
-
|
138
|
+
```ruby
|
117
139
|
JsonPath.for('{"candy":"lollipop"}').gsub('$..candy') {|v| "big turks" }.to_hash
|
118
|
-
|
140
|
+
```
|
119
141
|
|
120
142
|
The result will be
|
121
143
|
|
122
|
-
|
144
|
+
```ruby
|
123
145
|
{'candy' => 'big turks'}
|
124
|
-
|
146
|
+
```
|
125
147
|
|
126
148
|
If you'd like to remove all nil keys, you can use `#compact` and `#compact!`. To remove all keys under a certain path, use `#delete` or `#delete!`. You can even chain these methods together as follows:
|
127
149
|
|
128
|
-
|
150
|
+
```ruby
|
129
151
|
json = '{"candy":"lollipop","noncandy":null,"other":"things"}'
|
130
152
|
o = JsonPath.for(json).
|
131
153
|
gsub('$..candy') {|v| "big turks" }.
|
@@ -133,8 +155,8 @@ o = JsonPath.for(json).
|
|
133
155
|
delete('$..other').
|
134
156
|
to_hash
|
135
157
|
# => {"candy" => "big turks"}
|
136
|
-
|
158
|
+
```
|
137
159
|
|
138
160
|
# Contributions
|
139
161
|
|
140
|
-
Please feel free to submit an Issue or a Pull Request any time you feel like you would like to contribute. Thank you!
|
162
|
+
Please feel free to submit an Issue or a Pull Request any time you feel like you would like to contribute. Thank you!
|
data/Rakefile
CHANGED
data/lib/jsonpath.rb
CHANGED
@@ -16,7 +16,7 @@ class JsonPath
|
|
16
16
|
scanner = StringScanner.new(path)
|
17
17
|
@path = []
|
18
18
|
until scanner.eos?
|
19
|
-
if token = scanner.scan(
|
19
|
+
if token = scanner.scan(/\$\B|@\B|\*|\.\./)
|
20
20
|
@path << token
|
21
21
|
elsif token = scanner.scan(/[\$@a-zA-Z0-9:_-]+/)
|
22
22
|
@path << "['#{token}']"
|
data/lib/jsonpath/version.rb
CHANGED
data/test/test_jsonpath.rb
CHANGED
@@ -64,10 +64,16 @@ class TestJsonpath < MiniTest::Unit::TestCase
|
|
64
64
|
assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] > 20)]").on(@object)
|
65
65
|
end
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
67
|
+
def test_or_operator
|
68
|
+
assert_equal [@object['store']['book'][1], @object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] == 13 || @['price'] == 23)]").on(@object)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_and_operator
|
72
|
+
assert_equal [], JsonPath.new("$..book[?(@['price'] == 13 && @['price'] == 23)]").on(@object)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_and_operator_with_more_results
|
76
|
+
assert_equal [@object['store']['book'][1]], JsonPath.new("$..book[?(@['price'] < 23 && @['price'] > 9)]").on(@object)
|
71
77
|
end
|
72
78
|
|
73
79
|
def test_no_eval
|
@@ -99,7 +105,7 @@ class TestJsonpath < MiniTest::Unit::TestCase
|
|
99
105
|
end
|
100
106
|
|
101
107
|
def test_counting
|
102
|
-
assert_equal
|
108
|
+
assert_equal 54, JsonPath.new('$..*').on(@object).to_a.size
|
103
109
|
end
|
104
110
|
|
105
111
|
def test_space_in_path
|
@@ -231,6 +237,21 @@ class TestJsonpath < MiniTest::Unit::TestCase
|
|
231
237
|
assert_equal [], JsonPath.new("$.data[?(@.type == 'admins')]").on(data)
|
232
238
|
end
|
233
239
|
|
240
|
+
def test_support_at_sign_in_member_names
|
241
|
+
assert_equal [@object['store']['@id']], JsonPath.new("$.store.@id").on(@object)
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_support_dollar_sign_in_member_names
|
245
|
+
assert_equal [@object['store']['$meta-data']],
|
246
|
+
JsonPath.new("$.store.$meta-data").on(@object)
|
247
|
+
end
|
248
|
+
|
249
|
+
def test_support_underscore_in_member_names
|
250
|
+
assert_equal [@object['store']['_links']],
|
251
|
+
JsonPath.new("$.store._links").on(@object)
|
252
|
+
end
|
253
|
+
|
254
|
+
|
234
255
|
def example_object
|
235
256
|
{ 'store' => {
|
236
257
|
'book' => [
|
@@ -278,7 +299,10 @@ class TestJsonpath < MiniTest::Unit::TestCase
|
|
278
299
|
'single-speed' => 'no',
|
279
300
|
'2seater' => 'yes',
|
280
301
|
'make:model' => 'Zippy Sweetwheeler'
|
281
|
-
}
|
302
|
+
},
|
303
|
+
"@id" => "http://example.org/store/42",
|
304
|
+
"$meta-data" => "whatevs",
|
305
|
+
"_links" => { "self" => {} }
|
282
306
|
} }
|
283
307
|
end
|
284
308
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonpath
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Hull
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-05-
|
12
|
+
date: 2017-05-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: multi_json
|