render JSON array in HTTP node.js

89 Views Asked by At

I have a JSON string that I get from an express GET request which queries a mongo db.

res.write(string) gives me:

[
 {
  "_id":{
     "epc":"30742503C40AE4AE128918B1",
     "audit":109
  },
  "ss":{
     "x":1674,
     "y":96,
     "ts":"2016-05-09T02:24:03.000Z",
     "regions":[
        419,
        416,
        415,
        401
     ]
  },
  "v3":{
     "ts":"1970-01-01T00:00:00.000Z",
     "sts":"2016-05-09T16:10:16.549Z",
     "location":"A002R028S03100100"
  }
 }
]

However, I want to only render certain information from the string, for example: epc, audit, ss.ts, v3.ts and v3.location.

How do I do this?

1

There are 1 best solutions below

1
On BEST ANSWER

You can use res.json and filter by the desired fields.

res.json({ ss: string.ss.ts });
res.json({ v3: string.v3.ts });
// ... etc

Or you can use res.write and filter the results:

res.write(JSON.stringify(
    string.map(function (result){ 
         return {ss: result.ss.ts}; 
    })
));