简体   繁体   中英

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.

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

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

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) 

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

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

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) 

您可以使用reduce()使用单线执行此操作

let sum = words.reduce((a,c)=>a+parseFloat(c.low), 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;
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM