0

``I have a rest service which is reading a json string which is below listed. I am testing this through workbench and i am getting the following error.

Expected JSON object to deserialize apex parameter from at [line:4, column:7] errorCode: JSON_PARSER_ERROR

How do we debug this?

 {
      "courses":  
      [
        {
          "CourseId" : "301",
          "CourseName" : "Basic Hardwood Installation"
        },
        {
          "CourseId" : "401",
          "CourseName" : "Advanced Hardwood Installation"
        }
      ]
}

Here is my deserialized code. I was wondering whether i can deserialize the entire json in one code or should i be looping to get the value i want?

enter code here
JSONParser parser = JSON.createParser(courses);
while (parser.nextToken() != null) {
    // Start at the array of invoices.
    if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
        while (parser.nextToken() != null) {
            // Advance to the start object marker to
            //  find next invoice statement object.
            if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
                // Read entire invoice object, including its array of line items.
                CoursesWrapper courseWrapperObj = (CoursesWrapper)parser.readValueAs(CoursesWrapper.class);
                system.debug('Course Id: ' + courseWrapperObj.courseId);
                system.debug('CourseName: ' + courseWrapperObj.courseName);
                // For debugging purposes, serialize again to verify what was parsed.
                /*String s = JSON.serialize(inv);
                system.debug('Serialized invoice: ' + s);
                */

                // Skip the child start array and start object markers.
                parser.skipChildren();
            }
        }
    }
  • 2
    What does your object look like your trying to deserliaze into? – EricSSH Oct 14 '16 at 20:58
  • 1
    While the main answer's been answered, I would advise that you never, ever use JSONParser. It's more of a portability class. JSON.deserialize and JSON.deserializeUntyped is all you should need under normal circumstances. I've never found a case where JSONParser is easier or less verbose than JSON.deserializeUntyped in a worst-case scenario. – sfdcfox Oct 14 '16 at 21:38
2

It's almost end of day for me and I don't check this on the weekends usually, so I'm guessing your class that your deserilizing into is wrong, I think this would work for that response.

public class Courses
{
    public Courses()
    {
        courses = new List<CoursesData>();
    }

    public List<CoursesData> courses {get; set};
}

public class CoursesData 
{
    public String CourseId {get; set};  
    public String CourseName {get; set};        
}

Testing in execute anonymous

String json = '{"courses":[{"CourseId":"301","CourseName":"Basic Hardwood Installation"},{"CourseId":"401","CourseName":"Advanced Hardwood Installation"}]}';
Courses result = new Courses();
result = (Courses)System.JSON.deserialize(json,  Courses.class);   
system.debug('result ' + result);   
public class Courses
{
    public Courses()
    {
        courses = new List<CoursesData>();
    }

    public List<CoursesData> courses {get; set;}
}

public class CoursesData 
{
    public String CourseId {get; set;}  
    public String CourseName {get; set;}        
}

Yields

DEBUG|result Courses:[courses=(CoursesData:[CourseId=301, CourseName=Basic Hardwood Installation], CoursesData:[CourseId=401, CourseName=Advanced Hardwood Installation])]
  • Your CoursesData/Courses classes are far too verbose. There's no need for getters, setters, or constructors. – sfdcfox Oct 14 '16 at 21:36
  • @sfdcfox I get and set so in the future if there is a change there will be no impact for the consumer, I also build the constructor so CoursesData will never be null, but technically there is no need, it's personal preference – EricSSH Oct 14 '16 at 21:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.