How to filter JSON data in node.js?

I saw very old answers to this question, and many of the technologies used 2 years ago have changed.

I have JSON files sent by a database to my server and I would like to know how to filter this data.

I am starting the 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"'); 

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

+5
source share
2 answers

underscore has a where function that only does 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}] 

Another Lo-Dash utility library has a where function, which works the same way.


You can add underline to your project using

 $ npm install --save underscore 

or lodash

 $ npm install --save 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 

To use it in your project

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

Even if you use the library for this, it’s best to set up a thread chain that processes all your data processing in smaller modules.

Not knowing what you really want to do, I created this as an example. For the purposes of this code, perhaps think of a debug logging thread or something like that.

JSON-parser.js

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

input: object
output: object (result 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

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}] 

Here I created a Stringifier stream that converts objects back to JSON so that we can see how they are dumped to the console, although you can easily create any threads necessary to handle the operations your application requires. The endpoints of your stream will most likely not be written to the console.

As a final note, you are likely to create a database stream that takes some query parameters and emits json. You will pass this stream directly to parser .

In any case, I hope this gives you a better idea of ​​how to process data in node.js.

+22
source

You can use any of the usual built-in array / object functions that are in javascript, usually such a request will be made during the extraction of your data from the database, and not after.

sort of

 for(i=0;i<objIdsArray.length;i++) { for(j=0;j<mockJSON.length;j++) { if(mockJSON[j]["id"] === parseInt(objIdsArray[i])) { mockJSON.splice(j, 1); // to delete it, could be any other instruction } } } 
+2
source

Source: https://habr.com/ru/post/1201185/


All Articles