如何在node.js中过滤JSON数据? [英] How to filter JSON data in node.js?

查看:123
本文介绍了如何在node.js中过滤JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个问题,我看到了非常古老的答案,两年前使用的许多技术也都发生了变化.

I have seen very old answers to this question and many of the technologies used 2 years back have changed.

我所拥有的是数据库发送到服务器的JSON文件,我想知道如何过滤该数据.

What I have is JSON files sent by a database over to my server, and what I would like to know is how to filter that data.

我正在使用node.js运行服务器,而我想做的事情是这样的:

I am running a server with node.js, and what I would like to do is something like:

var results = QueryLibrary.load(jsondata);
var filtered = results.query('select where user = "user1"');

如何在节点中运行的javascript中做类似的事情?

How can I do something like this in javascript running in node?

推荐答案

下划线具有

underscore has a where function that does just this

var _ = require("underscore");

var json = '[{"user": "a", "age": 20}, {"user": "b", "age": 30}, {"user": "c", "age": 40}]';

var users = JSON.parse(json);

var filtered = _.where(users, {user: "a"});

// => [{user: "a", age: 20}]

另一个实用程序库 Lo-Dash 具有其中功能完全相同.

Another utility library, Lo-Dash, has a where function that operates identically.

您可以使用下划线将下划线添加到您的项目中

You can add underscore to your project using

$ npm install --save underscore

或lodash

$ npm install --save lodash


如果您关心where函数,lodash会将其作为单独的模块提供


If you only care about the where function, lodash offers it as a separate module

// only install lodash.where
$ npm install --save lodash.where

要在您的项目中使用

var where = require("lodash.where");

// ...
var filtered = where(users, {"user": "a"});


即使您使用库来执行此操作,一种更好的方法也可能是设置一连串的流,以在较小的模块中处理所有数据处理.


Even if you use a library to do this, a better approach is probably to setup a chain of streams that handles all of your data processing in smaller modules.

在不知道您实际要做什么的情况下,我创建了此示例.出于这段代码的目的,也许想到调试日志流或其他东西.

Without knowing what you actually want to do, I've created this as an example. For the purposes of this code, maybe think of a debug logging stream or something.

json-parser.js

输入:字符串(JSON)
输出:对象

input: string (JSON)
output: object

var Transform = require("stream").Transform;

function JsonParser() {
  Transform.call(this, {objectMode: true});
  this._transform = function _transform(json, enc, done) {
    try {
      this.push(JSON.parse(json));
    }
    catch (e) {
      return done(e);
    }
    done();
  }
}

JsonParser.prototype = Object.create(Transform.prototype, {
  constructor: {
    value: JsonParser
  }
});

module.exports = JsonParser;

obj-filter.js

输入:对象
输出:对象(结果为where(data, filters))

input: object
output: object (result of where(data, filters))

var Transform = require("stream").Transform;
var where = require("lodash.where");

function ObjFilter(filters) {
  Transform.call(this, {objectMode: true});
  this._transform = function _transform(obj, enc, done) {
    this.push(where(obj, filters));
    done();
  }
}

ObjFilter.prototype = Object.create(Transform.prototype, {
  constructor: {
    value: ObjFilter
  }
});

module.exports = ObjFilter;

stringifier.js

输入:对象
输出:字符串(JSON)

input: object
output: string (JSON)

var Transform = require("stream").Transform;

function Stringifier() {
  Transform.call(this, {objectMode: true});
  this._transform = function _transform(obj, enc, done) {
    this.push(JSON.stringify(obj));
    done();
  }
}

Stringifier.prototype = Object.create(Transform.prototype, {
  constructor: {
    value: Stringifier
  }
});

module.exports = Stringifier;

app.js

// modules
var JsonParser = require("json-parser");
var ObjFilter = require("obj-filter");
var Stringifier = require("stringifier");

// run
var parser = new JsonParser();

// setup stream chain
parser.pipe(new ObjFilter({"user": "a"}))
      .pipe(new Stringifier())
      .pipe(process.stdout);

// send example json in
parser.write('[{"user": "a", "age": 20}, {"user": "b", "age": 30}, {"user": "c", "age": 40}]');

// output
// => [{"user":"a","age":20}]

在这里,我创建了一个Stringifier流,该流将对象转换回JSON,以便我们可以看到它们已转储到控制台中,尽管您可以轻松创建处理应用程序所需操作所需的任何流.您的流端点可能不会最终写入控制台.

Here, I made a Stringifier stream that converts objects back into JSON so that we can see them dumped into the console, though you could easily create any streams you needed to handle the operations that your app requires. Your stream end points will likely not end up in writing to the console.

最后一点,您可能会创建一个接受某种查询选项并发出json的数据库流.您可以将该流直接通过管道传输到parser.

As a last note, you would probably create a database stream that accepts some sort of query options and emits json. You would pipe that stream directly into parser.

无论如何,我希望这可以使您更好地了解如何在node.js中处理数据.

Anyway, I hope this gives you a better idea of how to process data in node.js.

这篇关于如何在node.js中过滤JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆