Skip to content

Data Selector for JSON Payload #437

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

Merged
merged 12 commits into from
Feb 27, 2025
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"doctrine/dbal": "^2.12 || ^3.8",
"dragonmantank/cron-expression": "^3.1",
"league/flysystem-sftp-v3": "^3.0",
"mtdowling/jmespath.php": "^2.8",
"nesbot/carbon": "^2.72 || ^3.8.4",
"phpoffice/phpspreadsheet": "^2.2 || ^3.3",
"pimcore/admin-ui-classic-bundle": "^1.0 || ^2.0",
Expand Down
46 changes: 38 additions & 8 deletions src/DataSource/Interpreter/JsonFileInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@

namespace Pimcore\Bundle\DataImporterBundle\DataSource\Interpreter;

use JmesPath\Parser as Parser;
use JmesPath\Env as JmesPath;
use JmesPath\SyntaxErrorException;
use Pimcore\Bundle\DataImporterBundle\PimcoreDataImporterBundle;
use Pimcore\Bundle\DataImporterBundle\Preview\Model\PreviewData;
use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;

class JsonFileInterpreter extends AbstractInterpreter
{
protected string $path;

/**
* @var array|null
*/
Expand All @@ -30,15 +36,24 @@ class JsonFileInterpreter extends AbstractInterpreter
*/
protected $cachedFilePath = null;

protected function loadDataRaw(string $path): array
{
$content = file_get_contents($path);
return json_decode($this->prepareContent($content), true);
}

protected function loadData(string $path): array
{
if ($this->cachedFilePath === $path && !empty($this->cachedContent)) {
$content = file_get_contents($path);

return json_decode($this->prepareContent($content), true);
$data = $this->loadDataRaw($path);
} else {
return $this->cachedContent;
$data = $this->cachedContent;
}

if (!empty($this->path)) {
return $this->getValueFromPath($data);
}
return $data;
}

protected function doInterpretFileAndCallProcessRow(string $path): void
Expand All @@ -52,7 +67,16 @@ protected function doInterpretFileAndCallProcessRow(string $path): void

public function setSettings(array $settings): void
{
//nothing to do
$path = $settings['path'];
if (!empty($path)) {
try {
(new Parser)->parse($path);
} catch (SyntaxErrorException $e) {
throw new InvalidConfigurationException("Invalid JMESPath expression: " . $e->getMessage());
}
}

$this->path = $path;
}

/**
Expand Down Expand Up @@ -86,9 +110,7 @@ public function fileValid(string $path, bool $originalFilename = false): bool
}
}

$content = file_get_contents($path);

$data = json_decode($this->prepareContent($content), true);
$data = $this->loadDataRaw($path);

if (json_last_error() === JSON_ERROR_NONE) {
$this->cachedContent = $data;
Expand Down Expand Up @@ -132,4 +154,12 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo

return new PreviewData($columns, $previewData, $readRecordNumber, $mappedColumns);
}

/**
* Returns a value from the specified path in a nested array `$data`.
*/
private function getValueFromPath(array $data): mixed
{
return JmesPath::search($this->path, $data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pimcore.plugin.pimcoreDataImporterBundle.configuration.components.interpreter.js
},
border: false,
items: [
{
xtype: 'textfield',
fieldLabel: t('plugin_pimcore_datahub_data_importer_configpanel_json_path'),
name: this.dataNamePrefix + 'path',
value: this.data.path || '',
}
]
});
}
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ plugin_pimcore_datahub_data_importer_configpanel_csv_escape: Escape
plugin_pimcore_datahub_data_importer_configpanel_xlsx_sheet: Sheet
plugin_pimcore_datahub_data_importer_configpanel_xml_xpath: XPath
plugin_pimcore_datahub_data_importer_configpanel_xml_schema: Schema
plugin_pimcore_datahub_data_importer_configpanel_json_path: JMESPath
plugin_pimcore_datahub_data_importer_configpanel_import_settings: Import Settings
plugin_pimcore_datahub_data_importer_configpanel_import_preview: Import Preview
plugin_pimcore_datahub_data_importer_configpanel_preview_data_clone: Copy preview data from data source for preview.
Expand Down