2

I have the following code:

JSON STRING (recordIDListJSON):

[\"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.

APEX CODE:

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!

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

2 Answers 2

1

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.

3
  • 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
    Commented Nov 1, 2015 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
    Commented Nov 1, 2015 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. Commented Nov 1, 2015 at 23:50
0

I uses these tools:

1
  • 3
    Does this really answer the OP?
    – Uwe Heim
    Commented Nov 2, 2015 at 9:31

You must log in to answer this question.