1

I am using a SOAP API that returns XML but with JSON strings within the response envelope. For most of the API calls this has not been a problem but there is one that returns Javascript new Date objects that is causing problems when using JSON.parse. Here is a simplified example of the response I am getting.

"{\"History\":[ {\"Timestamp\":new Date(1380024020923)}]}"

When using JSON.parse I get the following error.

JSON::ParserError: 399: unexpected token at '{"Timestamp":new Date(1380024020923)}]}'

Is there a nice way to parse this string or am I going to have to use some regex/string trickery? Has anyone come across this way of returning a date object and I would like to understand the advantage?

cih
  • 1,960
  • 1
  • 16
  • 27
  • 1
    See this question (http://stackoverflow.com/questions/4511705/how-to-parse-json-to-receive-a-date-object-in-javascript) and look for the answer with dateTimeReviver.With a change in the regex, that should work for you. – Pete Sep 24 '13 at 17:35
  • 1
    That's not [JSON](http://json.org), that's a chunk of JavaScript source code. No, they're not the same thing. – mu is too short Sep 24 '13 at 17:48
  • @Pete thanks +1 for the link, what I understand from that is ideally the api would return the date as a string but in this case I will have to regex them, convert the dates to strings and then parse it as JSON to enable me to use it. Would you consider this an API bug? – cih Sep 24 '13 at 18:26
  • No, not a bug. It's simply a results of JSON standard not having a defined date format, so different vendors and users have created their own ways of passing dates. So not a bug, just an incomplete standard, in my mind... – Pete Sep 24 '13 at 18:30

0 Answers0