0

I'm receiving some JSON from an API. I created a Wrapper like this:

public class Wrapper{

    public class cA{

        List<cB> recordsB;

        public class cB{
            cC recordC;

            public class cC{
                String _some_id; // <--- Here I'm getting the error
            }
        }
    }
}

So, when I call it to my HTTP future class, I try to parse it with:
Wrapper.cA dataResponse = (Wrapper.cA)JSON.deserialize(input, Wrapper.cA.class);

And I get the error

"Invalid character in identifier".

Thing is that I cannot change the name of the comming field _some_id. Can anybody tell me how could I parse this child class in order to receive the name as it is?

Thanks a lot in advance!

2
  • 1
    See referenced Q&A. While a slightly different root cause, the workaround still applies in your case. As an alternative, you can deserialize via another method. See salesforce.stackexchange.com/questions/302034/… for suggestions.
    – identigral
    Oct 30, 2020 at 1:20
  • 1
    Hello! I cannot use replaceAll or any other Pattern, because the file I'm getting is too long and I get a "Regex too complicated" error. But the second URL with all the examples gave me the idea to try out the JSON.deserializeUntyped, and it worked! Oct 30, 2020 at 9:58

2 Answers 2

1

The Naming Convention for Variables/Attributes in Apex can't have any special character in the beginning. So, As identigral mentioned in the comment, You can either use JSONParser to get the values from the JSON body or an easier approach would be to simply replace the attribute from the JSON itself.

Wrapper.cA dataResponse = (Wrapper.cA)JSON.deserialize(input.replaceAll('\\_some_id','someId), Wrapper.cA.class);

then change the attribute name from your Wrapper class.

public class Wrapper{

    public class cA{

        List<cB> recordsB;

        public class cB{
            cC recordC;

            public class cC{
                String someId; 
            }
        }
    }
}

Another approach: Or if you want to implement JSONParser,

JSONParser parser = JSON.createParser(input);
      while (parser.nextToken() != null) {
        if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
          String fieldName = parser.getText();
          parser.nextToken();
          if (fieldName == '_some_id') {
            attributeValue = parser.getText(); //you'll get value of the attribute from JSON here...
          }
        }
      }
    }
1
  • Since I cannot use replaceAll, because I'll get a "Regex too complicated" error message (because the volume data I'm handling is too big), I've comed up with this solution that worked for me: Oct 30, 2020 at 10:00
1

At the end, I came up with this solution:

  • First, I remodelled the Wrapper class like this:
public class Wrapper{

    List<cA> recordsA;

    public class cA{
        cB recordB;

        public class cB{
            String _some_id; // <--- Here I'm getting the error
        }

    }
    
}
  • After that, I used JSON.deserializeUntyped to control the data:
Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(responseBody);
Wrapper dto;
List<cA> wrappers;

if(result.containsKey('recordsA')){
    wrappers = new List<cA> ();
    
    List<Object> jsonList = (List<Object>)result.get('recordsA');
    for(Object o : (List<Object>)jsonList){
        cA recordA = new cA();
        Map<String, Object> w = (Map<String, Object>)o;
            

        if(w.containsKey('___'))    recordA.___ = String.valueOf((Integer)(w.get('___'))); // <-- use as many times as you need for attributes in cA class

        if(w.containsKey('recordB')){
            cB recordB = new cB();
            Map<String, Object> pm = (Map<String, Object>)w.get('recordB');
                
            if(pm.containsKey('___'))   recordB.___ = (String)(pm.get('___'));  // <-- use as many times as you need for attributes in cB class
                
            recordA.recordB = recordB;
        }
        recordsA.add(recordA);
    }
}

Two links that helped me a lot:

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