5
possum
5y

So I had this JSON thingy, where I named the property containing a datetime string "timestamp".
For some reason, JS decided to convert that into a unix timestamp int on parse. Thx for nothing.

Comments
  • 0
    JSON.parse will not automatically convert UTC dates to epoch out of the box.
  • 0
    @jimshorts After I changed that property‘s name, the string remained intact after JSON.parse.
  • 1
    @possum right and im telling you that's not the default behavior of JSON.parse out of the box. I dunno what codebase you're working in, but it sounds like theres a modification living somewhere.

    > var date = new Date()
    undefined
    > date
    2019-07-06T07:36:16.763Z
    > var obj = JSON.stringify({"timestamp": date})
    undefined
    > obj
    '{"timestamp":"2019-07-06T07:36:16.763Z"}'
    > JSON.parse(obj)
    { timestamp: '2019-07-06T07:36:16.763Z' }
  • 0
    @jimshorts Yeah right, I was pretty surprised, too.
    It‘s an Electron application, the only addition to JS itself is jQuery (that probably won‘t affect the JSON methods)
  • 0
    @possum well I'd love more info because this is starting to look like a problem for stack overflow and not a rant ;). Feel free to post a link to the repo / offending code. You're not passing in a reviver to JSON.parse() are you?
  • 1
    @jimshorts nah I‘m just ranting, and it‘s my own local code that‘s not on any repo. I‘ll look the issue up too, I was just annoyed by that yesterday.
Add Comment