JSON: Cannot deserialize instance of date from VALUE_STRING

I’m officially breaking up with Salesforce Date parsing. This has got to be the most frustrating thing.

I have the following date in a JSON string (full JSON below):

2017-06-03T10:34:24.000Z

But when I try to deserialize the data:

Opportunity[] results = (List<Opportunity>)JSON.deserialize(res, List<Opportunity>.class);

I get the following error:

System.JSONException: Cannot deserialize instance of date from VALUE_STRING value 2016-12-05T16:19:44.000Z

I have looked at so many date parse issues before and from what I’ve gathered it must be in ISO 8601 – though ISO 8601 can take various shapes: Ruby Docs: ISO 8601

All of the following seem to recommend exactly what I’ve done

Cannot deserialize instance of datetime from VALUE_STRING value

https://developer.salesforce.com/forums/?id=906F00000009BsqIAE

https://github.com/heroku/databasedotcom/issues/22

https://github.com/heroku/databasedotcom/issues/35

How should I format this date string?

Thank you!

Full JSON string:

[{
  "name":"OpportunityABC",
  "CloseDate":"2017-06-03T10:34:24.000Z",
  "StageName":"Pre-Engagement",
  "Amount":"4810308.0",
}]

Answer

Three problems here:

(a) Date receiving DateTime value.

The value should be “YYYY-MM-DD” only.

(b) Decimal receiving String value.

The value should not be in quotes.

(c) You must not have a trailing comma.

JSON-compliant strings are more strict than normal JavaScript.

[{
  "name":"OpportunityABC",
  "CloseDate":"2017-06-03",
  "StageName":"Pre-Engagement",
  "Amount":4810308.0
}]

Attribution
Source : Link , Question Author : ZAR , Answer Author : sfdcfox

Leave a Comment