This code

List<String> ls = new List<String>{'32', '4text', 'text5'};
Object obj;
for (String s : ls){
        obj = JSON.deserializeUntyped(s); 
        System.debug(s + ' becomes ' + obj);
        System.debug(obj instanceof Integer);
    catch (JsonException e) { 
        System.debug(s + ' is not json' );

returns, or rather displays, to be exact

32 becomes 32
4text becomes 4
text5 is not json

I would expect 4text to be interpreted as String, not Integer. Maybe it's how JSON works but I didn't find it in RFC 8529 (I admit I just had a quick look)... Or is it a bug ?


1 Answer 1


A String in JSON is always wrapped in double quotes but your code does not provide those. Perhaps the RFC specifies that number parsing discards unrecognised trailing characters, hence your result?

Try out:

List<String> ls = new List<String>{'"32"', '"4text"', '"text5"'};
  • You're totally right. Unfortunately when you get data from external APIs you sometimes -if not often- get data like {"n" : "12", "hash" : "32af80d1"}. If you want n to be understood as the number 12, you'll have the hash understood as the number 32. The solution is to use well coded APIs but sometimes you just can't... Dec 14, 2020 at 13:42
  • In case anyone else gets the issue, you can check the string is really a number with Pattern.matches('^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$', serializedString), this way you won't deserialize as numbers strings like '32ef' or '42 years old' Dec 14, 2020 at 13:47

You must log in to answer this question.

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