简体   繁体   English

如何通过使用node.js从实时json数据中提取特定字段来创建新的单个json对象

[英]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. 我有以下代码,使用mqtt在指定的url中发布json数据。从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 以下是订阅者代码,它通过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. 在上面的代码中,我在'message'中的指定url中获取所有json数据。我需要从接收到的数据中提取'id'和'value'并将其作为单个JSON对象,并需要将其发布到mqtt,这样另一个客户端只能订阅'id'和'value'作为json数据。

To convert a JSON text into an object, you can use the eval() function. 要将JSON文本转换为对象,可以使用eval()函数。 eval() invokes the JavaScript compiler. eval()调用JavaScript编译器。 Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. 由于JSON是JavaScript的适当子集,因此编译器将正确解析文本并生成对象结构。 The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax. 文本必须包含在parens中,以避免绊倒JavaScript语法中的歧义。

var myObject = eval(message);

The eval function is very fast. eval功能非常快。 However, it can compile and execute any JavaScript program, so there can be security issues. 但是,它可以编译和执行任何JavaScript程序,因此可能存在安全问题。 The use of eval is indicated when the source is trusted and competent. 当源是可信的并且胜任时,指示使用eval。 It is much safer to use a JSON parser. 使用JSON解析器更安全。 In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. 在基于XMLHttpRequest的Web应用程序中,只允许与提供该页面的同一源进行通信,因此它是可信的。 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. 如果服务器的JSON编码不严格,或者它没有严格验证其所有输入,那么它可能会传送可能带有危险脚本的无效JSON文本。 The eval function would execute the script, unleashing its malice. eval函数将执行脚本,释放其恶意。

To defend against this, a JSON parser should be used. 为了防止这种情况,应该使用JSON解析器 A JSON parser will recognize only JSON text, rejecting all scripts. JSON解析器将仅识别JSON文本,拒绝所有脚本。 In browsers that provide native JSON support, JSON parsers are also much faster than eval. 在提供本机JSON支持的浏览器中,JSON解析器也比eval快得多。

var myObject = JSON.parse(message);

And use it as a Object: 并将其用作对象:

myObject.id;
myObject.value;

Create a object with just id and value : 创建一个只有id和value 的对象

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

Convert to JSON string: 转换为JSON字符串:

var jsonStr = JSON.stringify(idAndValueObj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM