1

I'm getting a JSON array from a REST callout:

[{\"name\":\"Jan0\",\"lastName\":\"Test0\",\"id\":0},
 {\"name\":\"Jan1\",\"lastName\":\"Test1\",\"id\":1},
 {\"name\":\"Jan2\",\"lastName\":\"Test2\",\"id\":2},
 {\"name\":\"Jan3\",\"lastName\":\"Test3\",\"id\":3}]

This gives me an error:

 List<Object> objs = (List<Object>)JSON.deserializeUntyped(res.getBody());

FATAL_ERROR System.TypeException: Invalid conversion from runtime type String to List

Ouch. Did I serialize the JSON incorrectly, or am I doing a mistake at covnerting the JSOn to the List?

2

1 Answer 1

3

Looks like the payload got serialized twice somehow. Ideally you would fix that so it is serialized just once instead. However, you can fix it in the interim by deserializing first into a String, then into your List<Object>.

String payload = (String)JSON.deserializeUntyped(response.getBody());
List<Object> data = (List<Object>)JSON.deserializeUntyped(payload);
3
  • Thanks a lot, you were right! May I ask how you figured this out? It does not seem obvious to me.
    – DevelBase2
    Mar 4, 2022 at 21:33
  • 1
    @DevelBase2 The escaped double-quotes is indicative of this type of thing. "regular" JSON doesn't do that, it'd just be [{"name":"Jan0","lastName":"Test0","id":0} ...]
    – Derek F
    Mar 4, 2022 at 21:35
  • @DerekF Hah, brilliant. Thanks!
    – DevelBase2
    Mar 4, 2022 at 21:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .