tel90
tel90

Reputation: 9

How to use JSON data in Node.js?

I have a file, called ETHBTC.json:

 [{
    "open": "0.06252000",
    "high": "0.06264700",
    "low": "0.06239800",
    "close": "0.06254100",
    "volume": "681.69300000",
    "timestamp": 1521575400000
},
{
    "open": "0.06253500",
    "high": "0.06270000",
    "low": "0.06242800",
    "close": "0.06261900",
    "volume": "371.99900000",
    "timestamp": 1521575700000
},
{
    "open": "0.06261500",
    "high": "0.06280000",
    "low": "0.06257500",
    "close": "0.06266200",
    "volume": "519.11000000",
    "timestamp": 1521576000000
},
...
]

I am trying to save the low value to a variable in Node.js so I can add all the low values together, etc:

for(item in words) {
   var lowTotal = 0;
   lowTotal += words.low;
}

But I have no luck.

I'm also having trouble with the console.log just to log the low variable.

Upvotes: 1

Views: 105

Answers (10)

YouneL
YouneL

Reputation: 8351

For..in is used to iterate over properties of an object. there are many ways to loop throw an array of object, the easiest is to use for. one last thing the lowTotal variable should be declared outside the loop:

var words= [{
    "open": "0.06252000",
    "high": "0.06264700",
    "low": "0.06239800",
    "close": "0.06254100",
    "volume": "681.69300000",
    "timestamp": 1521575400000
},
{
    "open": "0.06253500",
    "high": "0.06270000",
    "low": "0.06242800",
    "close": "0.06261900",
    "volume": "371.99900000",
    "timestamp": 1521575700000
},
{
    "open": "0.06261500",
    "high": "0.06280000",
    "low": "0.06257500",
    "close": "0.06266200",
    "volume": "519.11000000",
    "timestamp": 1521576000000
}];

var lowTotal = 0;
for(var i=0; i<words.length; i++){
  lowTotal += parseFloat(words[i].low);
}

console.log(lowTotal);

Upvotes: 0

Vijayanath Viswanathan
Vijayanath Viswanathan

Reputation: 8541

please use Object.values like below. Convert JSON to object though using 'JSON.Parse' method

let sum = 0;
Object.values(YOURJSONOBJECT).forEach(element => {

sum += parseFloat(element["low"]);

});

console.log(sum);

and result would be "0.18740099999999998", which is the sum of 'low' property

Upvotes: 1

Mats
Mats

Reputation: 26

As some other suggested. But brackets around your tickerdata to create an array.

Another suggestion.

var tickers = require('./JSONFILE.json')

for (const value of Object.values(tickers)) {

  console.log(value.low)

}

since you probably don't want to sum all low values.

Upvotes: 0

James
James

Reputation: 82096

You can reduce to give an initial value & increment per item in the array

const json = JSON.parse('...');
const lowTotal = json.reduce((val, item) => val += parseFloat(item.low), 0);

Example

const data = `[{
  "open": "0.06252000",
  "high": "0.06264700",
  "low": "0.06239800",
  "close": "0.06254100",
  "volume": "681.69300000",
  "timestamp": 1521575400000
},
{
  "open": "0.06253500",
  "high": "0.06270000",
  "low": "0.06242800",
  "close": "0.06261900",
  "volume": "371.99900000",
  "timestamp": 1521575700000
},
{
  "open": "0.06261500",
  "high": "0.06280000",
  "low": "0.06257500",
  "close": "0.06266200",
  "volume": "519.11000000",
  "timestamp": 1521576000000
}]`;

const json = JSON.parse(data);
const lowTotal = json.reduce((val, item) => val += parseFloat(item.low), 0);
console.log(`Total low=${lowTotal}`);

Upvotes: 0

Code_Panda
Code_Panda

Reputation: 5

I am not sure if you are having trouble reading json file but there are bugs in your code

var lowTotal = 0; should be outside for loop.

Parse String JSON object to float

Read item not words

 var lowTotal = 0;
for(item in words){
    lowTotal += parseFloat(item.low);
 }

Upvotes: 0

Bellian
Bellian

Reputation: 2140

First you need to parse the JSON file:

let fs = require('fs');
let content = fs.readFileSync('PathToFile').toString();

Then you need to parse it:

let jsonData = JSON.parse(content);

Then iterate over the elements. I recommend the for...of loop

let total = 0;
for(let element of jsonData){
    total += element.low
}

You can also use Array.prototype.map or Array.prototype.reduce but first stick to the basics.

Also please be sure to work on the right types:

your numbers in the JSON are saved as strings. You will have to convert them also:

let total = 0;
for(let element of jsonData){
    total += parseFloat(element.low);
}

Upvotes: 1

RomanY
RomanY

Reputation: 416

You should fix your code a bit:

let lowTotal = 0;
for(item in words){
    lowTotal += item.low;
}

Or you can do it in a bit different way:

let lowTotal = 0;
words.forEach(word => lowTotal += word.low);

Or the most elegant way:

let lowTotal = words.reduce((a, b) => {
    if(isNaN(a)) { a = a.low; }
    return a + b.low;
}

Upvotes: 0

TLP
TLP

Reputation: 1288

You can do this with a one-liner using reduce()

let sum = words.reduce((a,c)=>a+parseFloat(c.low), 0);

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You have several issues in your code:

  1. You have var lowTotal = 0; inside for which is incorrect
  2. Better to use a for loop so that you get each item of array rather than index of array as in for(item in words)
  3. low is a string type value so convert it to float type and add it to get the sum.

 var words = [{
    "open": "0.06252000",
    "high": "0.06264700",
    "low": "0.06239800",
    "close": "0.06254100",
    "volume": "681.69300000",
    "timestamp": 1521575400000
},
{
    "open": "0.06253500",
    "high": "0.06270000",
    "low": "0.06242800",
    "close": "0.06261900",
    "volume": "371.99900000",
    "timestamp": 1521575700000
},
{
    "open": "0.06261500",
    "high": "0.06280000",
    "low": "0.06257500",
    "close": "0.06266200",
    "volume": "519.11000000",
    "timestamp": 1521576000000
}];
var lowTotal = 0;
words.forEach(function(word){
  lowTotal += parseFloat(word.low);
});

console.log(lowTotal)

Upvotes: 0

Just iterate over the object and sum the total

var data =  [{
    "open": "0.06252000",
    "high": "0.06264700",
    "low": "0.06239800",
    "close": "0.06254100",
    "volume": "681.69300000",
    "timestamp": 1521575400000
},
{
    "open": "0.06253500",
    "high": "0.06270000",
    "low": "0.06242800",
    "close": "0.06261900",
    "volume": "371.99900000",
    "timestamp": 1521575700000
},
{
    "open": "0.06261500",
    "high": "0.06280000",
    "low": "0.06257500",
    "close": "0.06266200",
    "volume": "519.11000000",
    "timestamp": 1521576000000
}]
var lowTotal = 0;
data.map(a=>lowTotal+=+a.low)
console.log(lowTotal)

Upvotes: 0

Related Questions