@twin.org/data-processing-extractors 0.0.2-next.3 → 0.0.2-next.4

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.
@@ -42,10 +42,40 @@ class JsonPathExtractorConnector {
42
42
  extractValue(jsonObject, rule, outputObject) {
43
43
  try {
44
44
  const jsonNodes = jsonP3.query(rule.source, jsonObject);
45
+ const ruleParts = rule.source.split(".");
46
+ // Group values by their target path to handle multiple results correctly
47
+ const pathToValues = {};
45
48
  for (const jsonNode of jsonNodes) {
46
49
  const retainPathDepth = rule.retainPathDepth ?? 0;
47
50
  const fullTargetPath = `${rule.target}${retainPathDepth > 0 ? `.${jsonNode.location.slice(-retainPathDepth).join(".")}` : ""}`;
48
- core.ObjectHelper.propertySet(outputObject, fullTargetPath, core.Coerce.byType(jsonNode.value, rule.coerce));
51
+ const coercedValue = core.Coerce.byType(jsonNode.value, rule.coerce);
52
+ const nodeLocation = jsonNode.location;
53
+ // If the last part of the location before the target property is an integer
54
+ // and the corresponding rule part ends with [*], we are dealing with an array
55
+ const isArray = nodeLocation.length > 1 &&
56
+ ruleParts.length > 1 &&
57
+ core.Is.integer(nodeLocation[nodeLocation.length - 2]) &&
58
+ ruleParts[nodeLocation.length - 2].endsWith("[*]");
59
+ if (isArray) {
60
+ // If the value is set and not currently an array, convert it to one
61
+ if (!core.Is.empty(pathToValues[fullTargetPath]) && !core.Is.array(pathToValues[fullTargetPath])) {
62
+ pathToValues[fullTargetPath] = [pathToValues[fullTargetPath]];
63
+ }
64
+ // Make sure the array exists
65
+ pathToValues[fullTargetPath] ??= [];
66
+ // And add the new value
67
+ if (core.Is.array(pathToValues[fullTargetPath])) {
68
+ pathToValues[fullTargetPath].push(coercedValue);
69
+ }
70
+ }
71
+ else {
72
+ // For single values, just set the value directly
73
+ pathToValues[fullTargetPath] = coercedValue;
74
+ }
75
+ }
76
+ // Set the values in the output object
77
+ for (const [path, values] of Object.entries(pathToValues)) {
78
+ core.ObjectHelper.propertySet(outputObject, path, values);
49
79
  }
50
80
  }
51
81
  catch (err) {
@@ -1,4 +1,4 @@
1
- import { Guards, ObjectHelper, Coerce, GeneralError, BaseError } from '@twin.org/core';
1
+ import { Guards, Coerce, Is, ObjectHelper, GeneralError, BaseError } from '@twin.org/core';
2
2
  import { query } from 'json-p3';
3
3
 
4
4
  // Copyright 2024 IOTA Stiftung.
@@ -40,10 +40,40 @@ class JsonPathExtractorConnector {
40
40
  extractValue(jsonObject, rule, outputObject) {
41
41
  try {
42
42
  const jsonNodes = query(rule.source, jsonObject);
43
+ const ruleParts = rule.source.split(".");
44
+ // Group values by their target path to handle multiple results correctly
45
+ const pathToValues = {};
43
46
  for (const jsonNode of jsonNodes) {
44
47
  const retainPathDepth = rule.retainPathDepth ?? 0;
45
48
  const fullTargetPath = `${rule.target}${retainPathDepth > 0 ? `.${jsonNode.location.slice(-retainPathDepth).join(".")}` : ""}`;
46
- ObjectHelper.propertySet(outputObject, fullTargetPath, Coerce.byType(jsonNode.value, rule.coerce));
49
+ const coercedValue = Coerce.byType(jsonNode.value, rule.coerce);
50
+ const nodeLocation = jsonNode.location;
51
+ // If the last part of the location before the target property is an integer
52
+ // and the corresponding rule part ends with [*], we are dealing with an array
53
+ const isArray = nodeLocation.length > 1 &&
54
+ ruleParts.length > 1 &&
55
+ Is.integer(nodeLocation[nodeLocation.length - 2]) &&
56
+ ruleParts[nodeLocation.length - 2].endsWith("[*]");
57
+ if (isArray) {
58
+ // If the value is set and not currently an array, convert it to one
59
+ if (!Is.empty(pathToValues[fullTargetPath]) && !Is.array(pathToValues[fullTargetPath])) {
60
+ pathToValues[fullTargetPath] = [pathToValues[fullTargetPath]];
61
+ }
62
+ // Make sure the array exists
63
+ pathToValues[fullTargetPath] ??= [];
64
+ // And add the new value
65
+ if (Is.array(pathToValues[fullTargetPath])) {
66
+ pathToValues[fullTargetPath].push(coercedValue);
67
+ }
68
+ }
69
+ else {
70
+ // For single values, just set the value directly
71
+ pathToValues[fullTargetPath] = coercedValue;
72
+ }
73
+ }
74
+ // Set the values in the output object
75
+ for (const [path, values] of Object.entries(pathToValues)) {
76
+ ObjectHelper.propertySet(outputObject, path, values);
47
77
  }
48
78
  }
49
79
  catch (err) {
package/docs/changelog.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @twin.org/data-processing-extractors - Changelog
2
2
 
3
+ ## [0.0.2-next.4](https://github.com/twinfoundation/data-processing/compare/data-processing-extractors-v0.0.2-next.3...data-processing-extractors-v0.0.2-next.4) (2025-10-22)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * supporting array usage ([#20](https://github.com/twinfoundation/data-processing/issues/20)) ([d2c68cd](https://github.com/twinfoundation/data-processing/commit/d2c68cd3983622586cc073377545d6416b87a827))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/data-processing-models bumped from 0.0.2-next.3 to 0.0.2-next.4
16
+
3
17
  ## [0.0.2-next.3](https://github.com/twinfoundation/data-processing/compare/data-processing-extractors-v0.0.2-next.2...data-processing-extractors-v0.0.2-next.3) (2025-10-09)
4
18
 
5
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/data-processing-extractors",
3
- "version": "0.0.2-next.3",
3
+ "version": "0.0.2-next.4",
4
4
  "description": "Data extractor connector implementations",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@twin.org/core": "next",
18
- "@twin.org/data-processing-models": "0.0.2-next.3",
18
+ "@twin.org/data-processing-models": "0.0.2-next.4",
19
19
  "@twin.org/nameof": "next",
20
20
  "@twin.org/web": "next",
21
21
  "json-p3": "2.2.2"