Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through JSON with node.js

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 pm",         "Start_Value":  0.02,         "Abstract": 18.60,         "Editor": 65.20     }, {         "Timestamp": "04-29-11 04:22:39 pm",         "End_Value":  22.22,         "Text": 8.65,         "Common": 1.10,         "Editable": "true",         "Insert": 6.0     }] } 

The keys in data will not always be the same (i've just used examples, there are 20 different keys), and as such, I cannot set up my script to statically reference them to get the values.

Otherwise I could state

var value1 = json.data.Timestamp; var value2 = json.data.Start_Value; var value3 = json.data.Abstract; etc 

In the past i've used a simple foreach loop on the data node...

foreach ($json->data as $key => $val) {     switch($key) {         case 'Timestamp':             //do this;         case: 'Start_Value':             //do this     } } 

But don't want to block the script. Any ideas?

like image 541
crawf Avatar asked Apr 29 '11 07:04

crawf


People also ask

How do I iterate through a JSON file in node JS?

Iterating Over JSON With Root Node Inside your React app, create a data. js file and export the above response as an object. Note that both images and users are arrays, so you can use the map() method in JSX to iterate over them as shown below. You can also use regular for loops.

Can you loop through JSON?

To loop through a JSON array with JavaScript, we can use a for of loop. to loop through the json array with a for of loop. We assign the entry being looped through to obj . Then we get the value of the id property of the object in the loop and log it.

How do I iterate through a JSON object?

Use Object.values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.


2 Answers

You can iterate through JavaScript objects this way:

for(var attributename in myobject){     console.log(attributename+": "+myobject[attributename]); } 

myobject could be your json.data

like image 160
Van Coding Avatar answered Sep 26 '22 16:09

Van Coding


You may also want to use hasOwnProperty in the loop.

for (var prop in obj) {     if (obj.hasOwnProperty(prop)) {         switch (prop) {             // obj[prop] has the value         }     } } 

node.js is single-threaded which means your script will block whether you want it or not. Remember that V8 (Google's Javascript engine that node.js uses) compiles Javascript into machine code which means that most basic operations are really fast and looping through an object with 100 keys would probably take a couple of nanoseconds?

However, if you do a lot more inside the loop and you don't want it to block right now, you could do something like this

switch (prop) {     case 'Timestamp':         setTimeout(function() { ... }, 5);         break;     case 'Start_Value':         setTimeout(function() { ... }, 10);         break; } 

If your loop is doing some very CPU intensive work, you will need to spawn a child process to do that work or use web workers.

like image 26
mak Avatar answered Sep 23 '22 16:09

mak