JSON Parsing with Apex in Salesforce – Part I

by Sudipta Deb | Oct 15, 2014 | Apex, JSON, Salesforce, tdd, test class, unit testing | 2 comments

Here, I would like to share an example of very basic JSON parsing with Apex programming language in Salesforce.

MainWrapper Class:

public class MainWrapper {
public Integer studentId;
public String studentName;
public String address;

public MainWrapper(Integer studentId, String studentName, String address) {
this.studentId = studentId;
this.studentName = studentName;
this.address = address;
}
}

And now the test class to check whether JSON Parsing is working fine or not.

@isTest
private class TestMainWrapper {

static testMethod void testJsonParser() {
String jsonMessage = '{"studentId" : 1, "studentName" : "Sudipta Deb", "address" : "Kolkata, India"}';

JSONParser parser = JSON.createParser(jsonMessage);
MainWrapper m = (MainWrapper)parser.readValueAs(MainWrapper.class);

System.assertEquals(1, m.studentId);
System.assertEquals('Sudipta Deb', m.studentName);
System.assertEquals('Kolkata, India', m.address);
}
}

I will share more complex JSON Parsing very soon. Any feedback is always welcome.

Written by

Related Posts

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

For the last few years, Salesforce administrators, architects, and security teams have been preparing for a major platform change: the retirement of permissions managed directly through Profiles. Salesforce had previously announced plans to move organizations toward a Permission Set–led security model, with enforcement expected to begin around the Spring ’26 timeframe.

read more...

2 Comments

  1. Anonymous

    Really Helpful.

    Reply

Leave a Reply