1

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.

canals_bib_bit
1,2912 gold badges12 silver badges25 bronze badges
asked Mar 21, 2018 at 12:46
7
  • 2
    this is not valid JSON, put a [...] around it and first use JSON.parse(). Also you need to access words[item].low and put the var lowTotal = 0; in front of the for loop. Commented Mar 21, 2018 at 12:48
  • You need to give more information, do you want the value as a total? Or separately? The requirements are unclear. Commented Mar 21, 2018 at 12:48
  • @oklas not relevant to the question. Commented Mar 21, 2018 at 12:50
  • i would like one variable as a total Commented Mar 21, 2018 at 12:50
  • lowTotal is always reset to 0 at each iteration. Put it outside the loop Commented Mar 21, 2018 at 12:51

10 Answers 10

1

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);
}
answered Mar 21, 2018 at 12:55
Sign up to request clarification or add additional context in comments.

Comments

1

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

answered Mar 21, 2018 at 13:16

Comments

0

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)

answered Mar 21, 2018 at 12:53

Comments

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)

answered Mar 21, 2018 at 12:51

2 Comments

@James the + transform the string into float number
Apologies, did not see that it's very subtle. Personally I'd be more specific rather than relying on the quirks of the language but yes, this does work.
0

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

let sum = words.reduce((a,c)=>a+parseFloat(c.low), 0);
answered Mar 21, 2018 at 12:54

Comments

0

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;
}
answered Mar 21, 2018 at 12:54

Comments

0

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);
 }
answered Mar 21, 2018 at 12:52

Comments

0

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}`);

answered Mar 21, 2018 at 12:52

Comments

0

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.

answered Mar 21, 2018 at 13:00

Comments

0

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);

answered Mar 21, 2018 at 12:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.