Search code examples
jsonnode.jshttpmqttreal-time-data

How can i make a new single json object by extracting particular fields from realtime json data using node.js


I have the following code which publishes the json data in the specified url using mqtt.The initial data is retrieved from http.

var request = require('request');
var JSONStream = require('JSONStream');
var es = require('event-stream');
var mqtt = require('mqtt');
request({url: 'http://isaacs.couchone.com/registry/_all_docs'})
    .pipe(JSONStream.parse('rows.*'))
    .pipe(es.mapSync(function (data) {
    console.info(data);
    var client = mqtt.createClient(1883, 'localhost');
    client.publish('NewTopic', JSON.stringify(data));
    client.end();
return data;
}))

The following is the subscriber code which subscribes the data that is published (in the above code) through mqtt

var mqtt = require('mqtt');
var client = mqtt.createClient();
client.subscribe('NewTopic');
client.on('message', function(topic, message) {
  console.info(message);
});

In the above code, I get all json data in the specified url in 'message'.I need to extract 'id' and 'value' from the received data and make it as a single JSON object and need to publish it to mqtt,so that another client can subscribe only the 'id' and 'value' as json data.


Solution

  • To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax.

    var myObject = eval(message);
    

    The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. But it might not be competent. If the server is not rigorous in its JSON encoding, or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.

    To defend against this, a JSON parser should be used. A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval.

    var myObject = JSON.parse(message);
    

    And use it as a Object:

    myObject.id;
    myObject.value;
    

    Create a object with just id and value :

    var idAndValueObj = {};
    idAndValueObj.id = myObject.id;
    idAndValueObj.value = myObject.value;
    

    Convert to JSON string:

    var jsonStr = JSON.stringify(idAndValueObj);