2014-04-29 18:01:30 +02:00
|
|
|
|
|
|
|
<script type="text/x-red" data-template-name="json">
|
2017-05-23 10:35:37 +02:00
|
|
|
<div class="form-row">
|
2018-01-15 00:19:01 +01:00
|
|
|
<label for="node-input-action"><span data-i18n="json.label.action"></span></label>
|
|
|
|
<select style="width:70%" id="node-input-action">
|
|
|
|
<option value="" data-i18n="json.label.actions.toggle"></option>
|
|
|
|
<option value="str" data-i18n="json.label.actions.str"></option>
|
|
|
|
<option value="obj" data-i18n="json.label.actions.obj"></option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
|
|
<label data-i18n="json.label.property"></label>
|
2018-01-16 11:59:44 +01:00
|
|
|
<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">
|
2018-01-15 00:19:01 +01:00
|
|
|
</div>
|
|
|
|
<hr align="middle"/>
|
|
|
|
<div class="form-row node-json-to-json-options">
|
2017-05-23 15:18:09 +02:00
|
|
|
<label style="width:100%; border-bottom: 1px solid #eee;"><span data-i18n="json.label.o2j"></span></label>
|
|
|
|
</div>
|
2018-01-15 00:19:01 +01:00
|
|
|
<div class="form-row node-json-to-json-options" style="padding-left: 20px;">
|
2017-05-23 15:18:09 +02:00
|
|
|
<input style="width:20px; vertical-align:top; margin-right: 5px;" type="checkbox" id="node-input-pretty"><label style="width: auto;" for="node-input-pretty" data-i18n="json.label.pretty"></span>
|
2017-05-23 10:35:37 +02:00
|
|
|
</div>
|
2014-04-29 18:01:30 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/x-red" data-help-name="json">
|
2017-05-23 15:18:09 +02:00
|
|
|
<p>Converts between a JSON string and its JavaScript object representation, in either direction.</p>
|
2017-04-25 22:47:58 +02:00
|
|
|
<h3>Inputs</h3>
|
2017-05-23 15:18:09 +02:00
|
|
|
<dl class="message-properties">
|
|
|
|
<dt>payload<span class="property-type">object | string</span></dt>
|
|
|
|
<dd>A JavaScript object or JSON string.</dd>
|
|
|
|
</dl>
|
|
|
|
<h3>Outputs</h3>
|
|
|
|
<dl class="message-properties">
|
|
|
|
<dt>payload<span class="property-type">object | string</span></dt>
|
|
|
|
<dd>
|
|
|
|
<ul>
|
|
|
|
<li>If the input is a JSON string it tries to parse it to a JavaScript object.</li>
|
|
|
|
<li>If the input is a JavaScript object it creates a JSON string. The string can optionally be well-formatted.</li>
|
|
|
|
</ul>
|
|
|
|
</dd>
|
|
|
|
</dl>
|
2018-01-15 00:19:01 +01:00
|
|
|
<h3>Details</h3>
|
|
|
|
<p>By default, the node operates on <code>msg.payload</code>, but can be configured
|
|
|
|
to convert any message property.</p>
|
|
|
|
<p>The node can also be configured to ensure a particular encoding instead of toggling
|
|
|
|
between the two. This can be used, for example, with the <code>HTTP In</code>
|
|
|
|
node to ensure the payload is a parsed object even if an incoming request
|
|
|
|
did not set its content-type correctly for the HTTP In node to do the conversion.</p>
|
2018-01-15 00:37:39 +01:00
|
|
|
<p>If the node is configured to ensure the property is encoded as a String and it
|
|
|
|
receives a String, no further checks will be made of the property. It will
|
|
|
|
not check the String is valid JSON nor will it reformat it if the format option
|
|
|
|
is selected.</p>
|
2014-04-29 18:01:30 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
RED.nodes.registerType('json',{
|
|
|
|
category: 'function',
|
|
|
|
color:"#DEBD5C",
|
|
|
|
defaults: {
|
2017-05-23 10:35:37 +02:00
|
|
|
name: {value:""},
|
2018-01-16 11:59:44 +01:00
|
|
|
property: {value:"payload",required:true},
|
|
|
|
action: {value:""},
|
2017-07-04 14:44:37 +02:00
|
|
|
pretty: {value:false}
|
2014-04-29 18:01:30 +02:00
|
|
|
},
|
|
|
|
inputs:1,
|
|
|
|
outputs:1,
|
2016-06-12 23:37:38 +02:00
|
|
|
icon: "parser-json.png",
|
2014-04-29 18:01:30 +02:00
|
|
|
label: function() {
|
|
|
|
return this.name||"json";
|
|
|
|
},
|
|
|
|
labelStyle: function() {
|
|
|
|
return this.name?"node_label_italic":"";
|
2018-01-15 00:19:01 +01:00
|
|
|
},
|
|
|
|
oneditprepare: function() {
|
|
|
|
if (this.property === undefined) {
|
|
|
|
$("#node-input-property").val("payload");
|
|
|
|
}
|
|
|
|
$("#node-input-property").typedInput({default:'msg',types:['msg']});
|
|
|
|
$("#node-input-action").change(function() {
|
|
|
|
if (this.value === "" || this.value === "str") {
|
|
|
|
$(".node-json-to-json-options").show();
|
|
|
|
} else {
|
|
|
|
$(".node-json-to-json-options").hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$("#node-input-action").change();
|
2014-04-29 18:01:30 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|