0

I have used data_str = JSON.stringify(data); to convert into string. the data in json is

[{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454","val":"0"},{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523","val":"1"},{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524","val":"2"},{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525","val":"3"},{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"4"},{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"5"},{"temperSensorData":"28.489084691371357","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171530","timestamp3":"1493270171531","val":"6"},{"temperSensorData":"28.489084691371144","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171531","timestamp3":"1493270171532","val":"7"},{"temperSensorData":"28.48908469137181","temperSensorUnit":"celsius","timestamp":"1493270171431","timestamp2":"1493270171546","timestamp3":"1493270171546","val":"25"}]

Now I want to take timestamp value and perform some mathematical operations.How to convert this into Float values? Even Though I used parseInt(timestamp) and Number(timestamp), it is not working and showing me result: NaN.

3
  • What are you trying to do when you get NaN? Doing for example parseInt(data[0]["timestamp"] is just giving you a number, which is also a floating-point value. Commented May 2, 2017 at 5:10
  • you need a float values use parseFloat(data.timestamp) Commented May 2, 2017 at 5:10
  • Actually, the problem is I am storing JSON data in a .json file. Every time the data keep on adding up(real time data). So reading "timestamp" each time from a file is difficult. Commented May 3, 2017 at 5:27

5 Answers 5

2

Try parseFloat("28.489084691371996") function. It will help you.

parseFloat("28.489084691371996")

Let me know if you need further help.

Sign up to request clarification or add additional context in comments.

Comments

1

DEMO

var jsonObj = [{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454","val":"0"},{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523","val":"1"},{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524","val":"2"},{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525","val":"3"},{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"4"},{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"5"},{"temperSensorData":"28.489084691371357","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171530","timestamp3":"1493270171531","val":"6"},{"temperSensorData":"28.489084691371144","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171531","timestamp3":"1493270171532","val":"7"},{"temperSensorData":"28.48908469137181","temperSensorUnit":"celsius","timestamp":"1493270171431","timestamp2":"1493270171546","timestamp3":"1493270171546","val":"25"}];

console.log(jsonObj.map(item => parseFloat(item.timestamp)));

Comments

1

parseFloat() - Mozilla

var arr = [{
"temperSensorData": "28.489084691371996", "temperSensorUnit": "celsius", "timestamp": "1493270171424", "timestamp2": "1493270171454", "timestamp3": "1493270171454", "val": "0"
}, {
"temperSensorData": "28.48908469137112", "temperSensorUnit": "celsius", "timestamp": "1493270171426", "timestamp2": "1493270171522", "timestamp3": "1493270171523", "val": "1"
}, {
"temperSensorData": "28.489084691371186", "temperSensorUnit": "celsius", "timestamp": "1493270171426", "timestamp2": "1493270171523", "timestamp3": "1493270171524", "val": "2"
}, {
"temperSensorData": "28.489084691371595", "temperSensorUnit": "celsius", "timestamp": "1493270171426", "timestamp2": "1493270171524", "timestamp3": "1493270171525", "val": "3"
}, {
"temperSensorData": "28.48908469137168", "temperSensorUnit": "celsius", "timestamp": "1493270171428", "timestamp2": "1493270171529", "timestamp3": "1493270171529", "val": "4"
}, {
"temperSensorData": "28.489084691371684", "temperSensorUnit": "celsius", "timestamp": "1493270171428", "timestamp2": "1493270171529", "timestamp3": "1493270171529", "val": "5"
}, {
"temperSensorData": "28.489084691371357", "temperSensorUnit": "celsius", "timestamp": "1493270171429", "timestamp2": "1493270171530", "timestamp3": "1493270171531", "val": "6"
}, {
"temperSensorData": "28.489084691371144", "temperSensorUnit": "celsius", "timestamp": "1493270171429", "timestamp2": "1493270171531", "timestamp3": "1493270171532", "val": "7"
 }, {
"temperSensorData": "28.48908469137181", "temperSensorUnit": "celsius", "timestamp": "1493270171431", "timestamp2": "1493270171546", "timestamp3": "1493270171546", "val": "25"
 }]


var t = arr.map(x=>{
    return parseFloat(x.timestamp)
})

console.log(t)

Result

[ 1493270171424,
  1493270171426,
  1493270171426,
  1493270171426,
  1493270171428,
  1493270171428,
  1493270171429,
  1493270171429,
  1493270171431 ]

Comments

1

use parseFloat(data.timestamp) instead of parseInt and timestamp is the inner object value of data json array .you could call like this data.timestamp i was create some map function to separate timestamp and append with in a array

var data = [{
  "temperSensorData": "28.489084691371996",
  "temperSensorUnit": "celsius",
  "timestamp": "1493270171424",
  "timestamp2": "1493270171454",
  "timestamp3": "1493270171454",
  "val": "0"
}, {
  "temperSensorData": "28.48908469137112",
  "temperSensorUnit": "celsius",
  "timestamp": "1493270171426",
  "timestamp2": "1493270171522",
  "timestamp3": "1493270171523",
  "val": "1"
}, {
  "temperSensorData": "28.489084691371186",
  "temperSensorUnit": "celsius",
  "timestamp": "1493270171426",
  "timestamp2": "1493270171523",
  "timestamp3": "1493270171524",
  "val": "2"
}, {
  "temperSensorData": "28.489084691371595",
  "temperSensorUnit": "celsius",
  "timestamp": "1493270171426",
  "timestamp2": "1493270171524",
  "timestamp3": "1493270171525",
  "val": "3"
}, {
  "temperSensorData": "28.48908469137168",
  "temperSensorUnit": "celsius",
  "timestamp": "1493270171428",
  "timestamp2": "1493270171529",
  "timestamp3": "1493270171529",
  "val": "4"
}, {
  "temperSensorData": "28.489084691371684",
  "temperSensorUnit": "celsius",
  "timestamp": "1493270171428",
  "timestamp2": "1493270171529",
  "timestamp3": "1493270171529",
  "val": "5"
}, {
  "temperSensorData": "28.489084691371357",
  "temperSensorUnit": "celsius",
  "timestamp": "1493270171429",
  "timestamp2": "1493270171530",
  "timestamp3": "1493270171531",
  "val": "6"
}, {
  "temperSensorData": "28.489084691371144",
  "temperSensorUnit": "celsius",
  "timestamp": "1493270171429",
  "timestamp2": "1493270171531",
  "timestamp3": "1493270171532",
  "val": "7"
}, {
  "temperSensorData": "28.48908469137181",
  "temperSensorUnit": "celsius",
  "timestamp": "1493270171431",
  "timestamp2": "1493270171546",
  "timestamp3": "1493270171546",
  "val": "25"
}]

console.log(data.map(a=> parseFloat(a.timestamp)))

Comments

1

It´s a good way to convert the JSON to an object...

Sample:

data_str=JSON.parse('[{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454","val":"0"},{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523","val":"1"},{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524","val":"2"},{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525","val":"3"},{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"4"},{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"5"},{"temperSensorData":"28.489084691371357","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171530","timestamp3":"1493270171531","val":"6"},{"temperSensorData":"28.489084691371144","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171531","timestamp3":"1493270171532","val":"7"},{"temperSensorData":"28.48908469137181","temperSensorUnit":"celsius","timestamp":"1493270171431","timestamp2":"1493270171546","timestamp3":"1493270171546","val":"25"}]');
console.log (data_str.length);

console.log (data_str[0].temperSensorData);
console.log(parseFloat(data_str[0].temperSensorData));

myTimeStamp = parseInt(data_str[0].timestamp);
console.log(myTimeStamp);


var theDate = new Date(myTimeStamp);
dateString = theDate.toString();
console.log(dateString);

Comments

Your Answer

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.