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.
10 Answers 10
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);
}
Comments
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
Comments
You have several issues in your code:
- You have
var lowTotal = 0;insideforwhich is incorrect - Better to use a
forloop so that you get each item of array rather than index of array as infor(item in words) lowis astringtype value so convert it tofloattype 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)
Comments
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)
2 Comments
+ transform the string into float numberYou 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;
}
Comments
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);
}
Comments
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}`);
Comments
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.
Comments
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);
[...]around it and first useJSON.parse(). Also you need to accesswords[item].lowand put thevar lowTotal = 0;in front of the for loop.lowTotalis always reset to 0 at each iteration. Put it outside the loop