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

5
  • 9
    That isn't valid json Commented Jan 30, 2017 at 14:19
  • 1
    Try to use eval to get it as js object. Commented Jan 30, 2017 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. Commented Jan 30, 2017 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. Commented Jan 30, 2017 at 14:34
  • Possible duplicate of String to object in JS Commented Jan 30, 2017 at 20:46

1 Answer 1

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)

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

5 Comments

right...then convert to date object once received if that's what is required when data gets consumed
@charlietfl It looks like I could use this solution for that.
@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 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.
@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

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.