Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate jsonpath list from json #138

Merged
merged 10 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,42 @@ o = JsonPath.for(json).
# => {"candy" => "big turks"}
```

### Fetch all paths

To fetch all possible paths in given json, you can use `fetch_all_paths` method.

data:

```bash
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees"
},
{
"category": "fiction",
"author": "Evelyn Waugh"
}
]
}
```

... and this query:

```ruby
JsonPath.fetch_all_path(data)
```

... the result will be:

```bash
["$", "$.store", "$.store.book", "$.store.book[0].category", "$.store.book[0].author", "$.store.book[0]", "$.store.book[1].category", "$.store.book[1].author", "$.store.book[1]"]
```



# Contributions

Please feel free to submit an Issue or a Pull Request any time you feel like
Expand Down
1 change: 1 addition & 0 deletions jsonpath.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'code_stats'
s.add_development_dependency 'minitest', '~> 2.2.0'
s.add_development_dependency 'phocus'
s.add_development_dependency 'racc'
s.add_development_dependency 'rake'
end
33 changes: 33 additions & 0 deletions lib/jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ def on(obj_or_str, opts = {})
end
a
end

def self.fetch_all_path(obj)
all_paths = ['$']
find_path(obj, '$', all_paths, obj.class == Array)
return all_paths
end

def self.find_path(obj, root_key, all_paths, is_array = false)
obj.each do |key, value|
table_params = { key: key, root_key: root_key}
is_loop = value.class == Array || value.class == Hash
if is_loop
path_exp = construct_path(table_params)
all_paths << path_exp
find_path(value, path_exp, all_paths, value.class == Array)
elsif is_array
table_params[:index] = obj.find_index(key)
path_exp = construct_path(table_params)
find_path(key, path_exp, all_paths, key.class == Array) if key.class == Hash || key.class == Array
all_paths << path_exp
else
all_paths << construct_path(table_params)
end
end
end

def self.construct_path(table_row)
if table_row[:index]
return table_row[:root_key] + '['+ table_row[:index].to_s + ']'
else
return table_row[:root_key] + '.'+ table_row[:key]
end
end

def first(obj_or_str, *args)
enum_on(obj_or_str).first(*args)
Expand Down
15 changes: 15 additions & 0 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1152,4 +1152,19 @@ def example_object
'_links' => { 'self' => {} }
} }
end

def test_fetch_all_path
data = {
"foo" => nil,
"bar" => {
"baz" => nil
},
"bars" => [
{ "foo" => 12 },
{ "foo" => nil },
{ }
]
}
assert_equal ["$", "$.foo", "$.bar", "$.bar.baz", "$.bars", "$.bars[0].foo", "$.bars[0]", "$.bars[1].foo", "$.bars[1]", "$.bars[2]"], JsonPath.fetch_all_path(data)
end
end