I have the following code:


[\"a1mf0000000TU00AAG\", \"a1mf0000000TTzvAAG\", \"a1mf0000000KGFsAAO\"]"

This is a JSON.stringify of a JavaScript array. It contains record IDs which I want to assign to a List called recordIdList in my apex controller.


system.debug('Passed in value: '+ recordIdListJSON);
recordIdList = (List<String>)System.JSON.deserialize(recordIdListJSON,List<String>.class);
system.debug('Deserialized to: ' + recordIdList);

This is how I am trying to do it currently, but I'm getting the following error in the dev console logs:

FATAL_ERROR System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set

Can anybody tell me where I'm going wrong? Thanks!

  • What is the result of your system.debug('Passed in value: '+ recordIdListJSON); call? Nov 1 '15 at 23:22

I think you just need to sort out how you are defining your serialised string. Try this:

String recordIdListJSON = '[\"a1mf0000000TU00AAG\", \"a1mf0000000TTzvAAG\", \"a1mf0000000KGFsAAO\"]';

I've defined it with single quotes around the string - this works for the rest of your code and outputs the correct deserialised values.

  • Hi Caspar, when I pass in the string as per your answer I get the following: 23:05:36:066 FATAL_ERROR System.JSONException: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]
    – user24634
    Nov 1 '15 at 23:07
  • As response to above comment, I realised that it only works when the single quotes are removed, so it worked when it was [\"a1mf0000000TU00AAG\", \"a1mf0000000TTzvAAG\", \"a1mf0000000KGFsAAO\"]
    – user24634
    Nov 1 '15 at 23:35
  • 1
    It definitely depends on how you built the original list. If defined statically as in my example, I'd use single quotes, else, I'd not use any quotes. Hopefully the answer helped you get there at least. Nov 1 '15 at 23:50

I uses these tools:

  • 3
    Does this really answer the OP?
    – Uwe Heim
    Nov 2 '15 at 9:31

Your Answer

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