Support Property In/Out on the JSON Node

This commit is contained in:
Steve-Mcl 2024-04-12 11:01:26 +01:00
parent ef726e6a5f
commit 219b232cad
2 changed files with 22 additions and 10 deletions

View File

@ -1,5 +1,13 @@
<script type="text/html" data-template-name="json">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
<div class="form-row">
<label for="node-input-property"><i class="fa fa-sign-in"></i> <span data-i18n="common.label.propertyIn"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row">
<label for="node-input-action"><i class="fa fa-dot-circle-o"></i> <span data-i18n="json.label.action"></span></label>
<select style="width:70%" id="node-input-action">
@ -9,12 +17,8 @@
</select>
</div>
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="json.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
<label for="node-input-propertyOut"><i class="fa fa-sign-out"></i> <span data-i18n="common.label.propertyOut"></span></label>
<input type="text" id="node-input-propertyOut" style="width:70%;"/>
</div>
<hr align="middle"/>
<div class="form-row node-json-to-json-options">
@ -33,7 +37,10 @@
name: {value:""},
property: {value:"payload",required:true,
validate: RED.validators.typedInput({ type: 'msg', allowUndefined: true}),
label:RED._("node-red:json.label.property")},
label:RED._("node-red:common.label.propertyIn")},
propertyOut: {value:"payload",required:true,
validate: RED.validators.typedInput({ type: 'msg', allowUndefined: true}),
label:RED._("node-red:common.label.propertyOut")},
action: {value:""},
pretty: {value:false}
},
@ -50,7 +57,11 @@
if (this.property === undefined) {
$("#node-input-property").val("payload");
}
if (this.property === undefined) {
$("#node-input-propertyOut").val("payload");
}
$("#node-input-property").typedInput({default:'msg',types:['msg']});
$("#node-input-propertyOut").typedInput({default:'msg',types:['msg']});
$("#node-input-action").on("change", function() {
if (this.value === "" || this.value === "str") {
$(".node-json-to-json-options").show();

View File

@ -25,6 +25,7 @@ module.exports = function(RED) {
this.indent = n.pretty ? 4 : 0;
this.action = n.action||"";
this.property = n.property||"payload";
this.propertyOut = n.propertyOut||this.property;
this.schema = null;
this.compiledSchema = null;
@ -56,7 +57,7 @@ module.exports = function(RED) {
// else
if (node.action === "" || node.action === "obj") {
try {
RED.util.setMessageProperty(msg,node.property,JSON.parse(value));
RED.util.setMessageProperty(msg,node.propertyOut,JSON.parse(value));
if (validate) {
if (this.compiledSchema(msg[node.property])) {
delete msg.schema;
@ -95,7 +96,7 @@ module.exports = function(RED) {
try {
if (validate) {
if (this.compiledSchema(value)) {
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));
RED.util.setMessageProperty(msg,node.propertyOut,JSON.stringify(value,null,node.indent));
delete msg.schema;
send(msg);
done();
@ -104,7 +105,7 @@ module.exports = function(RED) {
done(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`);
}
} else {
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));
RED.util.setMessageProperty(msg,node.propertyOut,JSON.stringify(value,null,node.indent));
send(msg);
done();
}