Looping through JSON with node.js

前端 未结 9 1638
感情败类
感情败类 2020-12-08 00:04

I have a JSON file which I need to iterate over, as shown below...

{
    \"device_id\": \"8020\",
    \"data\": [{
        \"Timestamp\": \"04-29-11 05:22:39         


        
9条回答
  •  离开以前
    2020-12-08 00:44

    If we are using nodeJS, we should definitely take advantage of different libraries it provides. Inbuilt functions like each(), map(), reduce() and many more from underscoreJS reduces our efforts. Here's a sample

        var _=require("underscore");
        var fs=require("fs");
    
        var jsonObject=JSON.parse(fs.readFileSync('YourJson.json', 'utf8'));
    
    
        _.map( jsonObject, function(content) {
            _.map(content,function(data){
               if(data.Timestamp)
                  console.log(data.Timestamp)          
               })
          })
    

提交回复
热议问题