We have a problem with deserialize and serialize salesforce time fields Since Json and salesforce time fields both consist of "colon" we need to replace with a special character before deserialize and serialize.

Time t = DateTime.now().Time(); 
String s = JSON.serialize(t); 

System.debug(s); // "13:50:40.848Z"

Time t2 = (Time)JSON.deserialize(s, Time.class);

System.debug(t2); // 13:50:40.848Z

Test__Test1__c test = new Test__Test1__c(
    Test__Time__c = DateTime.now().Time()

String testString = JSON.serialize(test);


Test__Test1__c result = (Test__Test1__c)JSON.deserialize(testString, Test__Test1__c.class);

Please help me to resolve this

I got this error message

System.JSONException: Cannot deserialize instance of time from VALUE_STRING value 17:13:34.498Z or request may be missing a required field

  • what date format are you using in json? – Rahul Gawale Jun 20 '19 at 11:27
  • Basically any date is ok but time is the problem If serialize the datetime field into a string, I can not deserialize it. It will throw an error during deserialzatiob – JURY Jun 20 '19 at 11:36
  • can you share the code snippet over here? – Rahul Gawale Jun 20 '19 at 11:38
  • I updated the question with code – JURY Jun 20 '19 at 11:43
  • 1
    Time.parse deserialize doesn't support in Apex, so you have to customization for deserialize by using JSONparser, i achieved by using this salesforce.stackexchange.com/questions/220809/… – sdandamud1 Jun 20 '19 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.