* detect json better

This commit is contained in:
Ingo Fischer
2023-01-03 00:38:58 +01:00
parent 8fc6f4ac01
commit 62ef7cabf8
+10 -7
View File
@@ -40,16 +40,19 @@ function startAdapter(options) {
const target = {}; const target = {};
let requestFields = {}; let requestFields = {};
if (typeof state.val === 'string' && state.val.startsWith('{') && state.val.endsWith('}')) { if (typeof state.val === 'string') {
try { state.val = state.val.trim();
requestFields = JSON.parse(state.val) || {}; if (state.val.startsWith('{') && state.val.endsWith('}')) {
} catch (err) { try {
adapter.log.info(`Ignore data for service call ${id} is no valid JSON: ${err.message}`); requestFields = JSON.parse(state.val) || {};
requestFields = {}; } catch (err) {
adapter.log.info(`Ignore data for service call ${id} is no valid JSON: ${err.message}`);
requestFields = {};
}
} }
} }
// If a non-JSON value was set and we only have one relevant field, use this field as value // If a non-JSON value was set, and we only have one relevant field, use this field as value
if (fields && Object.keys(requestFields).length === 0) { if (fields && Object.keys(requestFields).length === 0) {
const fieldList = Object.keys(fields); const fieldList = Object.keys(fields);
if (fieldList.length === 1 && fieldList[0] !== 'entity_id') { if (fieldList.length === 1 && fieldList[0] !== 'entity_id') {