0

I have this answer from an API (written by me):

[[new Date(2017,0,28,16,00,00),[0.201766,0.201766,0.201766],[0.309878,0.309878,0.309878],[0.287467,0.287467,0.287467],[null,null,null], ...

I want my view to interpret this answer as javascript, but it doesn't. It assumes it is a string.

$.getJSON('http://localhost:XXXX/...', function (data) {

    // data.jsAlignedData == [[new Date(2017,0,28,16,00,00),[0.201766,0.201766,0.201766], ...

    console.log(data.jsAlignedData);
    // Shows it as a string

    var test = JSON.parse(data.jsAlignedData);
    // Error: Unexpected token e in JSON at position 3 
    // (IMO it's the 'e' from 'new')

    // it doesn't even reach this point:
    console.log(test);          
}

The final goal is to build a Dygraphs plot with a native array for increased speed (see "native array" in the documentation).

Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
  • 9
    That isn't valid json – charlietfl Jan 30 '17 at 14:19
  • 1
    Try to use [`eval`](http://www.w3schools.com/jsref/jsref_eval.asp) to get it as js object. – Mosh Feu Jan 30 '17 at 14:21
  • JSON objects can contain strings, numbers, booleans, `null`, arrays and other JSON objects. That's it - no `Date`, no JS syntax like `new`. – Joe Clay Jan 30 '17 at 14:23
  • @charlietfl Just to clarify (because the discussion under the other answer has just been deleted): the json is valid in the sense that `$.getJSON` does its work without exception. The tricky part is this one: as it can't interpret the array as a valid json (and this is where you are 100% right), it just presumes it's a string and it continues parsing the rest of the structure. – Xavier Peña Jan 30 '17 at 14:34
  • Possible duplicate of [String to object in JS](http://stackoverflow.com/questions/1086404/string-to-object-in-js) – user692942 Jan 30 '17 at 20:46

1 Answers1

1

You have to serialize to date using JSON standard (see https://en.wikipedia.org/wiki/ISO_8601 for example) to be able to parse it using JSON.parse().

Edit: "JSON standard" for date type is inaccurate because you will have to interpret the date string as a JS date once parsed... (as stated in @charlietfl comment)

Aloene
  • 326
  • 3
  • 12
  • right...then convert to date object once received if that's what is required when data gets consumed – charlietfl Jan 30 '17 at 14:25
  • @charlietfl It looks like I could use [this solution](http://stackoverflow.com/a/14509447/831138) for that. – Xavier Peña Jan 30 '17 at 14:37
  • @XavierPeña that is over complicating it ... just change the first array value from string to object when you receive the data `arr[0][0] = new Date(arr[0][0])` for example – charlietfl Jan 30 '17 at 14:39
  • @charlietfl The array is 75K chars long, and is formatted as `[[ date1, ... ],[ date2, ... ] ... [ dateN, ... ]]`. It contains ~400 dates. I could do a loop, but the idea was to make the backend did all the heavy work. – Xavier Peña Jan 30 '17 at 14:43
  • 1
    @XavierPeña iterating 400 items is not a big deal. It is very common to need to do data transformations also. You simply can not store a javascript date object in json – charlietfl Jan 30 '17 at 14:52