* 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 = {};
let requestFields = {};
if (typeof state.val === 'string' && state.val.startsWith('{') && state.val.endsWith('}')) {
try {
requestFields = JSON.parse(state.val) || {};
} catch (err) {
adapter.log.info(`Ignore data for service call ${id} is no valid JSON: ${err.message}`);
requestFields = {};
if (typeof state.val === 'string') {
state.val = state.val.trim();
if (state.val.startsWith('{') && state.val.endsWith('}')) {
try {
requestFields = JSON.parse(state.val) || {};
} 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) {
const fieldList = Object.keys(fields);
if (fieldList.length === 1 && fieldList[0] !== 'entity_id') {