json_data_extractor 0.0.1 → 0.0.2

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: a4263e61419466e8b50f31914c91697a628b1a0921c5bd3b4f783f81ee969f62
4
- data.tar.gz: f93d27bfca788b17431cabee2e36632e4b3b349592cc1d2e8889b835ac162d9e
3
+ metadata.gz: 4d73af1683f92224efd5eec46d590580ed837b40d86c9953e2a81f5a171de983
4
+ data.tar.gz: 211a5a1f2f20c0ff9c4948295e7e26fd9c62322dbc87e6263b9060e488b59a09
5
5
  SHA512:
6
- metadata.gz: ff5e1c2da47047683df1a108ace950e8d0fed4b0435360dedbf90af0a3d58ac11167da3eff6ce3fcd7fd6f705daef794a7942b96a6fb79175b8322c93218d242
7
- data.tar.gz: a6ff91e82661c4adc31a15ec600bd573f783c2b43da06ab17754712a7991047d9ea10c9f20fe8df5385428b9e7d3fef1bfb84fff145d57b5baf551f14d3b79b2
6
+ metadata.gz: 9558cc2ce6c7b4eeab0412ec8bae486925d402a0d6ce258eb3512c668fe0c7ea0be85a129523e33103b742182a8129bc5bad5796dcc292cefb1a9c595e2d93d9
7
+ data.tar.gz: 7f93de863e93f48a6c539f09257374d47b170265d83a88d8e2c7a9eba9e603f356828da5c110e4e7e27d336e5bebe883c8009f6c780103d057be3175c2b3e5e7
data/README.md CHANGED
@@ -1,7 +1,11 @@
1
1
  # JsonDataExtractor
2
2
 
3
- Another try to make something for JSON that is XSLT for XML.
4
- We transform one JSON into another JSON with the help of a third JSON!!!111!!eleventy!!
3
+ Transform JSON data structures with the help of a simple schema and JsonPath expressions.
4
+ Use the JsonDataExtractor gem to extract and modify data from complex JSON structures using a straightforward syntax
5
+ and a range of built-in or custom modifiers.
6
+
7
+ _Another try to make something for JSON that is XSLT for XML.
8
+ We transform one JSON into another JSON with the help of a third JSON!!!111!!eleventy!!_
5
9
 
6
10
  Remap one JSON structure into another with some basic rules and [jsonpath](https://github.com/joshbuddy/jsonpath).
7
11
 
@@ -25,9 +29,9 @@ Or install it yourself as:
25
29
 
26
30
  ## Usage
27
31
 
28
- Assuming you are familiar with [JSONPath](https://goessner.net/articles/JsonPath/), you can write simple mappers that will remap incoming data into the structure you need.
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.
29
33
 
30
- With the following source:
34
+ We'll base our examples on the following source:
31
35
 
32
36
  ```json
33
37
  {
@@ -68,7 +72,13 @@ With the following source:
68
72
  }
69
73
  ```
70
74
 
71
- and the following schema:
75
+ ### Defining a Schema
76
+
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.
78
+
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.
80
+
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:
72
82
 
73
83
  ```yaml
74
84
  schemas:
@@ -100,7 +110,7 @@ Modifiers can be supplied on object creation and/or added later by calling `#add
100
110
 
101
111
  ## TODO
102
112
 
103
- Update this readme for better usage cases.
113
+ Update this readme for better usage cases. Add info on arrays and modifiers.
104
114
 
105
115
 
106
116
  ## Development
@@ -1,33 +1,36 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "src/version"
3
+ require 'src/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "json_data_extractor"
8
- spec.version = JsonDataExtractor::VERSION
9
- spec.authors = ["Max Buslaev"]
10
- spec.email = ["max@buslaev.net"]
6
+ spec.name = 'json_data_extractor'
7
+ spec.version = JsonDataExtractor::VERSION
8
+ spec.authors = ['Max Buslaev']
9
+ spec.email = ['max@buslaev.net']
11
10
 
12
- spec.summary = %q{Transform JSON data structures with the help of simple schema.}
13
- spec.description = %q{Transform JSON data structures with the help of simple schema.}
14
- spec.homepage = "https://github.com/austerlitz/json_data_extractor"
15
- spec.license = "MIT"
11
+ spec.summary = %q{Transform JSON data structures with the help of a simple schema and JsonPath expressions.
12
+ Use the JsonDataExtractor gem to extract and modify data from complex JSON structures using a straightforward syntax
13
+ and a range of built-in or custom modifiers.}
14
+ spec.description = %q{json_data_extractor makes it easy to extract data from complex JSON structures,
15
+ such as API responses or configuration files, using a schema that defines the path to the data and any necessary
16
+ transformations. The schema is defined as a simple Ruby hash that maps keys to paths and optional modifiers.}
17
+ spec.homepage = 'https://github.com/austerlitz/json_data_extractor'
18
+ spec.license = 'MIT'
16
19
 
17
- # Specify which files should be added to the gem when it is released.
20
+ # Specify which files should be added to the gem when it is released.
18
21
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
22
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
23
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
24
  end
22
- spec.bindir = "exe"
25
+ spec.bindir = 'exe'
23
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
- spec.require_paths = ["lib"]
27
+ spec.require_paths = ['lib']
25
28
 
26
- spec.add_development_dependency "bundler", "~> 1.17"
27
- spec.add_development_dependency "rake", "~> 10.0"
28
- spec.add_development_dependency "rspec", "~> 3.0"
29
- spec.add_development_dependency "pry"
30
- spec.add_development_dependency "amazing_print"
29
+ spec.add_development_dependency 'bundler', '~> 1.17'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'amazing_print'
31
34
 
32
35
  spec.add_dependency 'jsonpath'
33
36
  end
@@ -1,31 +1,41 @@
1
1
  require "src/version"
2
2
 
3
3
  class JsonDataExtractor
4
- attr_reader :json_data, :modifiers
4
+ attr_reader :data, :modifiers
5
5
 
6
6
  def initialize(json_data, modifiers = {})
7
- @json_data = json_data
8
- @modifiers = modifiers
7
+ @data = json_data
8
+ @modifiers = modifiers.transform_keys(&:to_sym) # todo address this later
9
9
  end
10
10
 
11
+ # @param modifier_name [String, Symbol]
11
12
  def add_modifier(modifier_name, &block)
13
+ modifier_name = modifier_name.to_sym unless modifier_name.is_a?(Symbol)
12
14
  modifiers[modifier_name] = block
13
15
  end
14
16
 
17
+ # @param schema [Hash] schema of the expected data mapping
15
18
  def extract(schema)
16
19
  results = {}
17
20
  schema.each do |key, val|
18
- path = val.is_a?(Hash) && val['path'] ? val['path'] : val
19
- modifiers = val.is_a?(Hash) ? Array(val['modifiers'] || val['modifier']) : []
21
+ if val.is_a?(Hash)
22
+ val.transform_keys!(&:to_sym)
23
+ path = val[:path]
24
+ modifiers = Array(val[:modifiers] || val[:modifier]).map(&:to_sym)
25
+ array_type = 'array' == val[:type]
26
+ else
27
+ path = val
28
+ modifiers = []
29
+ end
20
30
 
21
- json_path = JsonPath.new(path)
22
- extracted_data = json_path.on(@json_data).flatten.compact
31
+ json_path = JsonPath.new(path)
32
+ extracted_data = json_path.on(@data)
23
33
 
24
34
  if extracted_data.empty?
25
35
  results[key] = nil
26
36
  else
27
37
  results[key] = apply_modifiers(extracted_data, modifiers)
28
- unless val.is_a?(Hash) && val['type'] && 'array' == val['type']
38
+ unless array_type
29
39
  results[key] = results[key].first unless 1 < results[key].size
30
40
  end
31
41
  end
@@ -39,11 +49,7 @@ class JsonDataExtractor
39
49
  data.map do |value|
40
50
  modified_value = value
41
51
  modifiers.each do |modifier|
42
- if @modifiers.key?(modifier.to_sym)
43
- modified_value = @modifiers[modifier.to_sym].call(modified_value)
44
- else
45
- modified_value = apply_single_modifier(modifier, modified_value)
46
- end
52
+ modified_value = apply_single_modifier(modifier, modified_value)
47
53
  end
48
54
  modified_value
49
55
  end
data/lib/src/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class JsonDataExtractor
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
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.1
4
+ version: 0.0.2
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-03-25 00:00:00.000000000 Z
11
+ date: 2023-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,7 +94,10 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: Transform JSON data structures with the help of simple schema.
97
+ description: |-
98
+ json_data_extractor makes it easy to extract data from complex JSON structures,
99
+ such as API responses or configuration files, using a schema that defines the path to the data and any necessary
100
+ transformations. The schema is defined as a simple Ruby hash that maps keys to paths and optional modifiers.
98
101
  email:
99
102
  - max@buslaev.net
100
103
  executables: []
@@ -136,5 +139,8 @@ requirements: []
136
139
  rubygems_version: 3.0.3
137
140
  signing_key:
138
141
  specification_version: 4
139
- summary: Transform JSON data structures with the help of simple schema.
142
+ summary: Transform JSON data structures with the help of a simple schema and JsonPath
143
+ expressions. Use the JsonDataExtractor gem to extract and modify data from complex
144
+ JSON structures using a straightforward syntax and a range of built-in or custom
145
+ modifiers.
140
146
  test_files: []