jsonwalk 1.1.1 → 1.1.2
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/README.md +98 -1
- data/jsonwalk-1.1.1.gem +0 -0
- data/lib/jsonwalk/version.rb +1 -1
- data/lib/jsonwalk.rb +5 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8ca16105bd17da8a1082049a7ab632974040680
|
4
|
+
data.tar.gz: b238a9f1055435af9e6a9fce2fce53f076510450
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15b46589fa5df9d53faeaaa3e2536f9b098a853c01214bfde27069ba9dfcd343b58a944e61edf019cfc9b217e2348397f91d5edbe6df17edfe9a58ea0bc4df3c
|
7
|
+
data.tar.gz: be4904e6a7ddab81cef1ccc1e60f32de46621236f9fce3ce8442ec6b4df33f82730067678fa5c2cbca38b11549d2f7a77d1698b65d958e901cf85984cda81da5
|
data/README.md
CHANGED
@@ -22,7 +22,104 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
jsonwalk provides functions to walk in a JSON tree created with JSON.parse
|
26
|
+
|
27
|
+
- JsonWalk.walk
|
28
|
+
selects the list values from a JSON tree using a given path of names
|
29
|
+
parameters :
|
30
|
+
root: the top of the JSON tree
|
31
|
+
usually the value returned by JSON.parse
|
32
|
+
path: the list of names which form the json path
|
33
|
+
each element may be a name, a selection, an integer or
|
34
|
+
the .. operator which causes a backward move
|
35
|
+
return :
|
36
|
+
the value or the list of values which match the path
|
37
|
+
|
38
|
+
- JsonWalk.forward
|
39
|
+
move into a JSON tree one step deeper
|
40
|
+
parameters :
|
41
|
+
node: a node or a list of nodes belonging to a JSON tree,
|
42
|
+
node may be an array. In this case all the values are returned
|
43
|
+
branch: the name of the object
|
44
|
+
output :
|
45
|
+
a list which contains the values corresponding to branch
|
46
|
+
nil if node is nil or branch has not been founded
|
47
|
+
- JsonWalk.select
|
48
|
+
filter out a node list based on the given operator
|
49
|
+
parameters :
|
50
|
+
node: a list of nodes belonging to a JSON tree
|
51
|
+
branch: the name of the object on which the selection will be done
|
52
|
+
condition: the string which must be matched by the value
|
53
|
+
operator: define how the name/value pair has to match
|
54
|
+
== value must be equal to condition
|
55
|
+
=~ value must match (regex) the condition
|
56
|
+
!= value must NOT be equal to condition
|
57
|
+
!~ value must NOT match (regex) the condition
|
58
|
+
output :
|
59
|
+
a list of nodes which match the condition
|
60
|
+
this is a subset of node
|
61
|
+
- JsonWalk.backward
|
62
|
+
move into a JSON tree one step back to the root
|
63
|
+
parameters :
|
64
|
+
parent: a single node of a JSON tree
|
65
|
+
node: a node or a list of nodes belonging to a JSON tree
|
66
|
+
output:
|
67
|
+
the list of the parent childern which include node as descendant
|
68
|
+
|
69
|
+
|
70
|
+
## Examples
|
71
|
+
Consider this Json object provided as the pjson2.txt file :
|
72
|
+
{
|
73
|
+
"food":
|
74
|
+
[
|
75
|
+
{
|
76
|
+
"id": "0001",
|
77
|
+
"type": "donut",
|
78
|
+
"ingredients": [
|
79
|
+
{
|
80
|
+
"flour": 200,
|
81
|
+
"eggs": 3,
|
82
|
+
}
|
83
|
+
]
|
84
|
+
},
|
85
|
+
{
|
86
|
+
"id": "0002",
|
87
|
+
"type": "cookie",
|
88
|
+
"ingredients": [
|
89
|
+
{
|
90
|
+
"flour": 100,
|
91
|
+
"chocolate": 50,
|
92
|
+
}
|
93
|
+
]
|
94
|
+
},
|
95
|
+
]
|
96
|
+
}
|
97
|
+
|
98
|
+
You can use the file test/cli_jsonwalk.rb to parse it with the syntax:
|
99
|
+
cli_jsonwalk.rb pjson2.txt path
|
100
|
+
|
101
|
+
1. get the value corresponding to the name food :
|
102
|
+
$ ruby test_jsonwalk.rb pjson2.txt food
|
103
|
+
["food"] ==> [{"id"=>"0001", "type"=>"donut", "ingredients"=>[{"flour"=>200}, {"eggs"=>3}]}, {"id"=>"0002", "type"=>"cookie", "ingredients"=>[{"flour"=>100}, {"chocolate"=>50}]}]
|
104
|
+
|
105
|
+
2. get the values corresponding to the path food,type
|
106
|
+
$ ruby test_jsonwalk.rb pjson2.txt food/type
|
107
|
+
["food", "type"] ==> ["donut", "cookie"]
|
108
|
+
|
109
|
+
3. get the ingredients for the elements of type donut
|
110
|
+
$ ruby test_jsonwalk.rb pjson2.txt food/type==donut/ingredients
|
111
|
+
["food", "type==donut", "ingredients"] ==> [[{"flour"=>200}, {"eggs"=>3}]]
|
112
|
+
|
113
|
+
4. get the type of elements which needs chocolate
|
114
|
+
$ ruby test_jsonwalk.rb pjson2.txt food/ingredients/chocolate/../../type
|
115
|
+
["food", "ingredients", "chocolate", "..", "..", "type"] ==> ["cookie"]
|
116
|
+
|
117
|
+
5. get the second ingredient for the type donut
|
118
|
+
$ ruby test_jsonwalk.rb pjson2.txt food/type==donut/ingredients/0/1
|
119
|
+
["food", "type==donut", "ingredients", 0, 1] ==> {"eggs"=>3}
|
120
|
+
|
121
|
+
Note that the selection type==donut does not remove the food array but propagate it
|
122
|
+
|
26
123
|
|
27
124
|
## Development
|
28
125
|
|
data/jsonwalk-1.1.1.gem
ADDED
Binary file
|
data/lib/jsonwalk/version.rb
CHANGED
data/lib/jsonwalk.rb
CHANGED
@@ -39,9 +39,8 @@ module JsonWalk
|
|
39
39
|
# if it is an array, return the set of children
|
40
40
|
def JsonWalk.forward(node, branch)
|
41
41
|
# move forward : get the uniq child or all of them
|
42
|
-
return
|
43
|
-
|
44
|
-
node.key?(branch) ? node[branch] : nil
|
42
|
+
return nil if node==nil
|
43
|
+
return node.kind_of?(Array) ? node.flatten.map {|child| child[branch]} : node[branch]
|
45
44
|
end
|
46
45
|
|
47
46
|
# just select a set of children matching a condition
|
@@ -62,6 +61,7 @@ module JsonWalk
|
|
62
61
|
|
63
62
|
# move one step backward (but keep current selection)
|
64
63
|
def JsonWalk.backward(parent,node)
|
64
|
+
return nil if parent==nil or node==nil
|
65
65
|
return parent.select { |child| IsAncestor(child, node) }
|
66
66
|
end
|
67
67
|
|
@@ -91,11 +91,11 @@ module JsonWalk
|
|
91
91
|
# move deeper in the tree
|
92
92
|
print "moving forward to key #{child_key}\n" if VERBOSE
|
93
93
|
levels.push nodes # keep track of current level
|
94
|
-
nodes =
|
94
|
+
nodes = forward( nodes, child_key)
|
95
95
|
end
|
96
96
|
|
97
97
|
end
|
98
|
-
return nodes
|
98
|
+
return nodes.class==Array ? nodes.compact : nodes
|
99
99
|
end
|
100
100
|
|
101
101
|
# module end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonwalk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Philippe Jounin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- bin/console
|
56
56
|
- bin/setup
|
57
57
|
- jsonwalk-1.1.0.gem
|
58
|
+
- jsonwalk-1.1.1.gem
|
58
59
|
- jsonwalk.gemspec
|
59
60
|
- lib/jsonwalk.rb
|
60
61
|
- lib/jsonwalk/version.rb
|