jsonpath/node-red-contrib-jsonpath.js
function JSONPathNode(n) {
        // Create a RED node
        RED.nodes.createNode(this,n);

        // Store local copies of the node configuration (as defined in the .html)
        this.expression = n.expression;
        this.split = n.split;

        var node = this;

        this.on("input", function(msg) {
            if ( msg.hasOwnProperty("payload") ) {
                var input = msg.payload;
                if (typeof msg.payload === "string") {
                    // It's a string: parse it as JSON
                    try {
                        input = JSON.parse(msg.payload);
                    } catch (e) {
                        node.warn("The message received is not JSON. Ignoring it.");
                        return;
                    }
                }
                // Evalute the JSONPath expresssion
                var evalResult = jsonPath.eval(input, node.expression);

                if (!node.split) {
                    // Batch it in one message. Carry pre-existing properties
                    msg.payload = evalResult;
                    node.send(msg);
                } else {
                    // Send one message per match result
                    var response = evalResult.map(function (value) {
                        return {"payload": value};
                    });
                    node.send([response]);
                }
            }
        });


        this.on("close", function() {
            // Called when the node is shutdown - eg on redeploy.
            // Allows ports to be closed, connections dropped etc.
            // eg: this.client.disconnect();
        });
    }
function JSONPathNode(n) {
        // Create a RED node
        RED.nodes.createNode(this,n);

        // Store local copies of the node configuration (as defined in the .html)
        this.expression = n.expression;
        this.split = n.split;

        var node = this;

        this.on("input", function(msg) {
            if ( msg.hasOwnProperty("payload") ) {
                var input = msg.payload;
                if (typeof msg.payload === "string") {
                    // It's a string: parse it as JSON
                    try {
                        input = JSON.parse(msg.payload);
                    } catch (e) {
                        node.warn("The message received is not JSON. Ignoring it.");
                        return;
                    }
                }
                // Evalute the JSONPath expresssion
                var evalResult = jsonPath.eval(input, node.expression);

                if (!node.split) {
                    // Batch it in one message. Carry pre-existing properties
                    msg.payload = evalResult;
                    node.send(msg);
                } else {
                    // Send one message per match result
                    var response = evalResult.map(function (value) {
                        return {"payload": value};
                    });
                    node.send([response]);
                }
            }
        });


        this.on("close", function() {
            // Called when the node is shutdown - eg on redeploy.
            // Allows ports to be closed, connections dropped etc.
            // eg: this.client.disconnect();
        });
    }
function JSONPathNode ( n ) { // Create a RED node RED . nodes . createNode ( this , n ) ; // Store local copies of the node configuration (as defined in the .html) this . expression = n . expression ; this . split = n . split ; var node = this ; this . on ( "input" , function ( msg ) { if ( msg . hasOwnProperty ( "payload" ) ) { var input = msg . payload ; if ( typeof msg . payload === "string" ) { // It's a string: parse it as JSON try { input = JSON . parse ( msg . payload ) ; } catch ( e ) { node . warn ( "The message received is not JSON. Ignoring it." ) ; return ; } } // Evalute the JSONPath expresssion var evalResult = jsonPath . eval ( input , node . expression ) ; if ( ! node . split ) { // Batch it in one message. Carry pre-existing properties msg . payload = evalResult ; node . send ( msg ) ; } else { // Send one message per match result var response = evalResult . map ( function ( value ) { return { "payload" : value } ; } ) ; node . send ( [ response ] ) ; } } } ) ; this . on ( "close" , function ( ) { // Called when the node is shutdown - eg on redeploy. // Allows ports to be closed, connections dropped etc. // eg: this.client.disconnect(); } ) ; }
The main node definition - most things happen in here
The main node definition - most things happen in here