json_data_extractor 0.0.12 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -1
- data/lib/json_data_extractor.rb +6 -3
- data/lib/src/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb96ee66beec96f117282087fadea7c8bf91febb79eddf8d588bfb26a4301466
|
4
|
+
data.tar.gz: b0a5221e3eb012da640233fac6fb5ff6a41715327de6cb381bd8896e184095bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48e16964d90fc098e3d2b1c9ad5136f642546a9fa04121aea8b2ed28847224fe85ee43a576476e61fbcdf501e0ae188488c23cdd1bab6bf9d1af9781d301df60
|
7
|
+
data.tar.gz: 8b7fd89bbd4ecd36b24a542112da82baeccc789e8cf86b8ce8cbbbf3444d28c2073809ba105cb96f261d88c847ca74ba31d8cbeacfe95d74d79a22b2af913907
|
data/README.md
CHANGED
@@ -120,6 +120,35 @@ The resulting json will be:
|
|
120
120
|
|
121
121
|
```
|
122
122
|
|
123
|
+
|
124
|
+
### Handling Default Values
|
125
|
+
|
126
|
+
With JsonDataExtractor, you can specify default values in your schema for keys that might be absent in the input JSON. Use the `path` and `default` keys in the schema for this purpose.
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
schema = {
|
130
|
+
absent_value: { path: nil },
|
131
|
+
default: { path: '$.some_real_path', default: 'foo' },
|
132
|
+
default_with_lambda: { path: '$.table', default: -> { 'DEFAULT' } },
|
133
|
+
absent_with_default: { path: nil, default: 'bar' }
|
134
|
+
}
|
135
|
+
```
|
136
|
+
|
137
|
+
- `absent_value`: Will be `nil` in the output as there's no corresponding key in the input JSON and no default is provided.
|
138
|
+
- `default`: Will either take the value from `$.some_real_path` in the input JSON or 'foo' if the path does not exist.
|
139
|
+
- `default_with_lambda`: Will take the value from `$..table` in the input JSON or 'DEFAULT' if the path does not exist.
|
140
|
+
- `absent_with_default`: Will be 'bar' in the output as there's no corresponding key in the input JSON but a default is provided.
|
141
|
+
|
142
|
+
#### Simplified Syntax for Absent Values
|
143
|
+
|
144
|
+
For keys that you expect to be absent in the input JSON but still want to include in the output with a `nil` value, you can use a simplified syntax by setting the schema value to `nil`.
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
schema = {
|
148
|
+
absent_value: nil
|
149
|
+
}
|
150
|
+
```
|
151
|
+
|
123
152
|
### Modifiers
|
124
153
|
|
125
154
|
Modifiers can be supplied on object creation and/or added later by calling `#add_modifier` method.
|
@@ -191,7 +220,7 @@ schema = {
|
|
191
220
|
map: FUEL_TYPES
|
192
221
|
}
|
193
222
|
}
|
194
|
-
result =
|
223
|
+
result = JsonDataExtractor.new(data).extract(schema) # => {"fuel":["Petrol","Diesel",nil,"Unknown"]}
|
195
224
|
```
|
196
225
|
|
197
226
|
A map is essentially a dictionary that defines key-value pairs, where the keys represent the source
|
data/lib/json_data_extractor.rb
CHANGED
@@ -20,9 +20,11 @@ class JsonDataExtractor
|
|
20
20
|
def extract(schema)
|
21
21
|
results = {}
|
22
22
|
schema.each do |key, val|
|
23
|
+
default_value = nil
|
23
24
|
if val.is_a?(Hash)
|
24
25
|
val.transform_keys!(&:to_sym)
|
25
26
|
path = val[:path]
|
27
|
+
default_value = val[:default]
|
26
28
|
maps = Array([val[:maps] || val[:map]]).flatten.compact.map do |map|
|
27
29
|
if map.is_a?(Hash)
|
28
30
|
map
|
@@ -48,11 +50,12 @@ class JsonDataExtractor
|
|
48
50
|
maps = []
|
49
51
|
end
|
50
52
|
|
51
|
-
extracted_data = JsonPath.on(@data, path)
|
53
|
+
extracted_data = JsonPath.on(@data, path) if path
|
52
54
|
|
53
|
-
if extracted_data.empty?
|
54
|
-
results[key] = nil
|
55
|
+
if extracted_data.nil? || extracted_data.empty?
|
56
|
+
results[key] = default_value.is_a?(Proc) ? default_value.call : (default_value || nil)
|
55
57
|
else
|
58
|
+
extracted_data.map! { |val| val.nil? ? default_value : val }
|
56
59
|
transformed_data = apply_modifiers(extracted_data, modifiers)
|
57
60
|
results[key] = apply_maps(transformed_data, maps)
|
58
61
|
|
data/lib/src/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_data_extractor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Buslaev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|