json bindings

This commit is contained in:
2026-06-13 20:19:36 +03:00
parent d623ee797c
commit 9eb257d237
10 changed files with 1024 additions and 24 deletions
+31 -21
View File
@@ -173,7 +173,8 @@ error_log("Loaded JSON import template: " . print_r($template, true));
<div class="mb-3 text">
<a href="imported.php?id=<?= $id ?>" class="btn btn-warning me-2">Imported (i)</a>
<a href="tolims.php?id=<?= $id ?>" class="btn btn-success">To LIMS (l)</a>
<a href="tolims.php?id=<?= $id ?>" class="btn btn-success me-2">To LIMS (l)</a>
<a href="bindings_manage.php?template_id=<?= $id ?>" class="btn btn-outline-secondary">Binding JSON -> LIMS</a>
</div>
<div class="card radius-10">
@@ -384,42 +385,51 @@ error_log("Loaded JSON import template: " . print_r($template, true));
}
function addJsonRow(jsonPayload, reference, sourceType) {
const normalizedPayload = normalizeJsonPayload(jsonPayload);
const flattened = flattenJson(normalizedPayload);
// Ogni elemento di data[] diventa una riga (non colonne data.0.*, data.1.*).
const records = extractRecords(jsonPayload);
if (INCLUDE_SOURCE_CODE_COLUMN) {
flattened.source_code = reference;
flattened.source_type = sourceType;
}
records.forEach((record, recordIndex) => {
const flattened = flattenJson(record);
const newColumns = Object.keys(flattened).filter(col => !columns.includes(col));
columns = columns.concat(newColumns);
const rowReference = records.length > 1 ?
reference + '#' + (recordIndex + 1) :
reference;
jsonRows.push({
excelrow: 'JSON-' + (jsonRows.length + 1),
reference: reference,
sourceType: sourceType,
flat: flattened
if (INCLUDE_SOURCE_CODE_COLUMN) {
flattened.source_code = rowReference;
flattened.source_type = sourceType;
}
const newColumns = Object.keys(flattened).filter(col => !columns.includes(col));
columns = columns.concat(newColumns);
jsonRows.push({
excelrow: 'JSON-' + (jsonRows.length + 1),
reference: rowReference,
sourceType: sourceType,
flat: flattened
});
});
renderTable();
}
function normalizeJsonPayload(payload) {
// Record da trasformare in righe: gli oggetti in data[], altrimenti il payload stesso.
function extractRecords(payload) {
if (
UNWRAP_SINGLE_DATA_ITEM &&
payload &&
typeof payload === 'object' &&
!Array.isArray(payload) &&
Array.isArray(payload.data) &&
payload.data.length === 1 &&
payload.data[0] &&
typeof payload.data[0] === 'object'
payload.data.length > 0
) {
return payload.data[0];
const items = payload.data.filter(item => item && typeof item === 'object');
if (items.length > 0) {
return items;
}
}
return payload;
return [payload];
}
function flattenJson(value, prefix = '', result = {}) {