I receive this json through a request

"pay_names[]":["name one","name two"],

I'm trying to deserialize it with this class

global class Famrs{
    global class datajsonweb{
       global String pay_CCIban {get; set;}
       global String pay_bic {get; set;}
       global List<String> pay_names {get; set;}
       global String pay_extid {get; set;}
    datajsonweb data = (datajsonweb)JSON.deserialize(requestBody.toString(),datajsonweb.class);
    Integer lstSize = data.pay_names.size(); //I get nullpointer here

I get an attempt to de-reference null object when I try to get the size of the array inside the json, i don't know what I'm doing wrong

Any help?

Thanks in advance


Like sfdcfox says, the "pay_names[]" key in the json needs to be like "pay_names" so I'm trying to do a replaceAll('[]','') but I'm getting: Invalid regex: Unclosed character class. If I try to escape the brackets I get: Illegal string literal.

Is it possible to remove the brackets in the json?

Thanks again


Thanks to Jesse Milburn's comment it works.

  • Are you sure that every json object contains data for that field? Or even contains that field? If the field is empty (or doesn't exist) the deserializer will not instantiate an empty list. Commented Feb 26, 2020 at 13:23
  • Yes completely sure, I get all data correct except for this: "pay_names[]":["name one","name two"],
    – Javier
    Commented Feb 26, 2020 at 13:31
  • 1
    Why does your pay_names json variable is called "pay_names[]"? Shouldn't it be "pay_names"? Commented Feb 26, 2020 at 13:38
  • I think that too, but I receive pay_names[]
    – Javier
    Commented Feb 26, 2020 at 13:46
  • 2
    \\[\\] should be your escape. Commented Feb 26, 2020 at 14:02

1 Answer 1


The [] in your JSON won't work. The parser would try to map this to an apex variable named pay_names[]. This is not a valid Apex identifier. Valid identifiers in Apex must start with a letter, can only include letters, numbers, and underscores, can't have two consecutive underscores, and can't be certain reserved keywords. JSON.deserialize does not automatically map things like [] to the nearest valid identifier.

In order to use a JSON class, your JSON would need to be:

"pay_names":["name one","name two"],

If you want to use the JSON exactly as is, use an explicit parser, or use JSON.deserializeUntyped, or JSON.Parser.

  • I think that too, I'm trying now to make a replaceAll('[]','') to remove the brackets in pay_names but I'm getting invalid regex: Unclosed character class near index 2 ].... Updated question
    – Javier
    Commented Feb 26, 2020 at 13:49

You must log in to answer this question.

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