I have created a parser class to parse some JSON into an object. This is accessible as a REST based service.

This is the class I parse to:

global class statParser {

    global class Request {
    global String sfId;
    global List<MResult> mResult;
    global List<TResult> tResult;

global class MResult {
    global String strsfId;
    global String Score;

global class VResult {
    global String message;
    global String mapping;

 global Request request;

Here is the class that does the parsing. It contains this function:

public static statParser parse(String json) {
    return (statParser) System.JSON.deserialize(json, statParser.class);

And here is the relevant code for manipulation:

global static void UpdateFromService(String M_JSON){

statParser eb_res = parse(M_JSON);



My problem is that whenever I call this function directly when executing anonymous apex using this string

{ "request": { "sfId": "001M000000yJLryIAG", "mResult": [], "vResult": []}}

It parses correctly and I can see the primitives in the class that have data. But when I try to to call it by POSTing the same JSON string to the function as @HttpPost (it's setup as a REST based service that does the sae thing as the code above) I get a generic JSON parser error which I can't figure our why. I'm sending the data as Content-Type: application/json . Any idea why the JSON parser error shows up?

  • Pretty sure that JSON is case sensitive. Have you verified that your mResult and vResult keys are correct? I can see MResult on the object and mResult on the JSON.... same for vResult. – Caspar Harmer May 19 '17 at 1:32
  • But JSON parser would simply ignore the string, if they are not exact match with custom variable names in the Inner class. What JSON error are you getting? – Salesforcesmarty May 19 '17 at 1:35
  • I think every variable in your inner class must be present in the JSON payload (or vice versa, can't remember from top of my head) if you're deserialising that way. – Boris Bachovski May 19 '17 at 2:01
  • 1
    @BorisBachovski - Thats only with Deserialize Strict. It changed way back in API v2x something – Eric May 19 '17 at 2:02
  • 3
    @akarnid - You provided us with all the code that DOES work but provided nothing that does not. Please update with your code you are using to POST, the HttpPost rest resource method, the debug of the JSON string in that method, etc. Then we can help. Most likely there is something is how you are passing the string to the post etc but no way to tell unless we see that code. And may need the parse method as well since that is not shown either – Eric May 19 '17 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.