json_data_extractor 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d73af1683f92224efd5eec46d590580ed837b40d86c9953e2a81f5a171de983
4
- data.tar.gz: 211a5a1f2f20c0ff9c4948295e7e26fd9c62322dbc87e6263b9060e488b59a09
3
+ metadata.gz: 900265f2ab694f6f2bad9365934d726747c42ab6161abd89cee954c1b8cc09b5
4
+ data.tar.gz: 73d2574fc410ed737df045d9933f69d3f9049323342c521db97be1e6c07ab213
5
5
  SHA512:
6
- metadata.gz: 9558cc2ce6c7b4eeab0412ec8bae486925d402a0d6ce258eb3512c668fe0c7ea0be85a129523e33103b742182a8129bc5bad5796dcc292cefb1a9c595e2d93d9
7
- data.tar.gz: 7f93de863e93f48a6c539f09257374d47b170265d83a88d8e2c7a9eba9e603f356828da5c110e4e7e27d336e5bebe883c8009f6c780103d057be3175c2b3e5e7
6
+ metadata.gz: 6f07161d8a3d53022dc387031349f486b731024d18beb1bd1e8522971f08194cc1e5e47863555539e263351a567b83d29e78920a24e9fb339e71c68e84ac3593
7
+ data.tar.gz: 739d2626d2092b3d72ee3c287ea4f9d6e9353f46c52d6f2256c56301939cff112cb059a4a1321b1b4ef576bc9841f09b4f7598edd855f1cd3c5491a521de5957
data/README.md CHANGED
@@ -1,10 +1,12 @@
1
1
  # JsonDataExtractor
2
2
 
3
+ NOTE: This is still a very early beta.
4
+
3
5
  Transform JSON data structures with the help of a simple schema and JsonPath expressions.
4
6
  Use the JsonDataExtractor gem to extract and modify data from complex JSON structures using a straightforward syntax
5
7
  and a range of built-in or custom modifiers.
6
8
 
7
- _Another try to make something for JSON that is XSLT for XML.
9
+ _Another try to make something for JSON that is XSLT for XML.
8
10
  We transform one JSON into another JSON with the help of a third JSON!!!111!!eleventy!!_
9
11
 
10
12
  Remap one JSON structure into another with some basic rules and [jsonpath](https://github.com/joshbuddy/jsonpath).
@@ -29,7 +31,9 @@ Or install it yourself as:
29
31
 
30
32
  ## Usage
31
33
 
32
- JsonDataExtractor allows you to remap one JSON structure into another with some basic rules and [JSONPath](https://goessner.net/articles/JsonPath/) expressions. The process involves defining a schema that maps the input JSON structure to the desired output structure.
34
+ JsonDataExtractor allows you to remap one JSON structure into another with some basic rules
35
+ and [JSONPath](https://goessner.net/articles/JsonPath/) expressions. The process involves defining a schema that maps
36
+ the input JSON structure to the desired output structure.
33
37
 
34
38
  We'll base our examples on the following source:
35
39
 
@@ -74,11 +78,15 @@ We'll base our examples on the following source:
74
78
 
75
79
  ### Defining a Schema
76
80
 
77
- A schema is defined using YAML syntax and consists of one or more mappings that specify how to extract data from the input JSON and where to place it in the output JSON.
81
+ A schema consists of one or more mappings that specify how to extract data from the input JSON and where to place it in
82
+ the output JSON.
78
83
 
79
- Each mapping has a path field that specifies the JsonPath expression to use for data extraction, and an optional modifier field that specifies one or more modifiers to apply to the extracted data. Modifiers are used to transform the data in some way before placing it in the output JSON.
84
+ Each mapping has a path field that specifies the JsonPath expression to use for data extraction, and an optional
85
+ modifier field that specifies one or more modifiers to apply to the extracted data. Modifiers are used to transform the
86
+ data in some way before placing it in the output JSON.
80
87
 
81
- Here's an example schema that extracts the authors and categories from a JSON structure similar to the one used in the previous example:
88
+ Here's an example schema that extracts the authors and categories from a JSON structure similar to the one used in the
89
+ previous example (here it's in YAML just for readability):
82
90
 
83
91
  ```yaml
84
92
  schemas:
@@ -87,7 +95,9 @@ schemas:
87
95
  modifier: downcase
88
96
  categories: $..category
89
97
  ```
98
+
90
99
  The resulting json will be:
100
+
91
101
  ```json
92
102
  {
93
103
  "authors": [
@@ -106,22 +116,50 @@ The resulting json will be:
106
116
 
107
117
  ```
108
118
 
109
- Modifiers can be supplied on object creation and/or added later by calling `#add_modifier` method. Please see specs for examples.
119
+ Modifiers can be supplied on object creation and/or added later by calling `#add_modifier` method. Please see specs for
120
+ examples.
121
+
122
+ ### Nested schemas
123
+
124
+ JDE supports nested schemas. Just provide your element with a type of `array` and add a `schema` key for its data.
125
+
126
+ E.g. this is a valid real-life schema with nested data:
127
+
128
+ ```ruby
129
+ {
130
+ name: '$.Name',
131
+ code: '$.Code',
132
+ services: '$.Services[*].Code',
133
+ locations: {
134
+ path: '$.Locations[*]',
135
+ type: 'array',
136
+ schema: {
137
+ name: '$.Name',
138
+ type: '$.Type',
139
+ code: '$.Code'
140
+ }
141
+ }
142
+ }
143
+ ```
110
144
 
111
145
  ## TODO
112
146
 
113
147
  Update this readme for better usage cases. Add info on arrays and modifiers.
114
148
 
115
-
116
149
  ## Development
117
150
 
118
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
151
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
152
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
119
153
 
120
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
154
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
155
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
156
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
121
157
 
122
158
  ## Contributing
123
159
 
124
- Bug reports and pull requests are welcome on GitHub at https://github.com/austerlitz/json_data_extractor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
160
+ Bug reports and pull requests are welcome on GitHub at https://github.com/austerlitz/json_data_extractor. This project
161
+ is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
162
+ the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
125
163
 
126
164
  ## License
127
165
 
@@ -129,4 +167,6 @@ The gem is available as open source under the terms of the [MIT License](https:/
129
167
 
130
168
  ## Code of Conduct
131
169
 
132
- Everyone interacting in the JsonDataExtractor project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/austerlitz/json_data_extractor/blob/master/CODE_OF_CONDUCT.md).
170
+ Everyone interacting in the JsonDataExtractor project’s codebases, issue trackers, chat rooms and mailing lists is
171
+ expected to follow
172
+ the [code of conduct](https://github.com/austerlitz/json_data_extractor/blob/master/CODE_OF_CONDUCT.md).
@@ -1,4 +1,5 @@
1
- require "src/version"
1
+ require 'src/version'
2
+ require 'jsonpath'
2
3
 
3
4
  class JsonDataExtractor
4
5
  attr_reader :data, :modifiers
@@ -23,6 +24,7 @@ class JsonDataExtractor
23
24
  path = val[:path]
24
25
  modifiers = Array(val[:modifiers] || val[:modifier]).map(&:to_sym)
25
26
  array_type = 'array' == val[:type]
27
+ nested = val.delete(:schema)
26
28
  else
27
29
  path = val
28
30
  modifiers = []
@@ -35,8 +37,15 @@ class JsonDataExtractor
35
37
  results[key] = nil
36
38
  else
37
39
  results[key] = apply_modifiers(extracted_data, modifiers)
38
- unless array_type
40
+
41
+ # TODO yeah, this looks ugly. Address it later.
42
+ if !array_type
39
43
  results[key] = results[key].first unless 1 < results[key].size
44
+ elsif nested
45
+ results[key] = []
46
+ Array(extracted_data).each do |item|
47
+ results[key] << self.class.new(item).extract(nested)
48
+ end
40
49
  end
41
50
  end
42
51
  end
data/lib/src/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class JsonDataExtractor
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_data_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Buslaev