function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AaranAaran 

JSON to Apex class

Hi, 

 

I'm new to salesforce and don't know much about it.  I would like to create apex classes for the following json string and deserialize it.  can somebody help me to write the apex class?  thank you..

 

{

  "name" : {

    "givenName" : "smith",

    "familyName" : "peter",

    "formatted" : "Smith Peter"

  },

  "location" : "london",

  "type" : "person",

  "displayName" : "smith peter",

  "status" : "xyz gets recognized by industry analyst as a leader...",

  "tags" : [ "leadership", "financial", "analysis", "capital", "formation", "contract", "talent", "management", "entrepreneur" ],

  "published" : "2011-11-01T20:17:08.667+0000",

  "followingCount" : 64,

  "thumbnailUrl" : "https://xyz.com",

  "jive" : {

    "level" : {

      "name" : "Contributor",

      "description" : "",

      "points" : 65

    },

    "timeZone" : "America/New_York",

    "locale" : "en_US",

    "username" : "psmith",

    "profile" : [ {

      "value" : "President & CEO",

      "jive_label" : "Title"

    }, {

      "value" : "11/06/2011",

      "jive_label" : "Join Date"

    }, {

      "value" : "lived in NY ",

      "jive_label" : "Biography"

    }, {

      "value" : "",

      "jive_label" : "Expertise"

    }, {

      "value" : [ "Financial Services ", "Business Services " ],

      "jive_label" : "Industries"

    }, {

      "value" : [ "Finance ", "Business Development ", "Consulting ", "Business Analysis ", "Marketing " ],

      "jive_label" : "Disciplines"

    }, {

      "value" : ".",

      "jive_label" : "What do you do?"

    }, {

      "value" : "team.",

      "jive_label" : "My Style"

    }, {

      "value" : "http://www.xyz.com",

      "jive_label" : "Website"

    }, {

      "value" : "",

      "jive_label" : "Business Manager"

    }, {

      "value" : "https://twitter.com/#!/x",

      "jive_label" : "Twitter"

    } ],

    "external" : false,

    "visible" : true,

    "enabled" : true,

    "externalContributor" : false,

    "federated" : false

  },

  "updated" : "2013-11-12T04:57:20.140+0000",

  "emails" : [ {

    "value" : "xyz.com@localhost",

    "type" : "work",

    "jive_label" : "Email",

    "primary" : true

  } ],

  "photos" : [ {

    "value" : "https://xy.com"

  } ],

  "thumbnailId" : "1093",

  "followerCount" : 50,

  "resources" : {

    "following" : {

      "ref" : "xy.xom",

      "allowed" : [ "GET" ]

    },

    "followers" : {

      "ref" : "xy.com",

      "allowed" : [ "GET" ]

    },

    

    "activity" : {

      "ref" : "https://xys.com",

      "allowed" : [ "GET" ]

    }

  },

  "id" : "2010"

}

sfdcfoxsfdcfox
AaranAaran

Thanks Brian for your quick response.  I'm getting this error message when I save the code:  

 

Type cannot be constructed: String at "tags.add(new String(parser));"

 

here is the code from http://json2apex.herokuapp.com/

 

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {
	public static void consumeObject(JSONParser parser) {
		Integer depth = 0;
		do {
			JSONToken curr = parser.getCurrentToken();
			if (curr == JSONToken.START_OBJECT || 
				curr == JSONToken.START_ARRAY) {
				depth++;
			} else if (curr == JSONToken.END_OBJECT ||
				curr == JSONToken.END_ARRAY) {
				depth--;
			}
		} while (depth > 0 && parser.nextToken() != null);
	}

	public class Name {
		public String givenName {get;set;}
		public String familyName {get;set;}
		public String formatted {get;set;}

		public Name(JSONParser parser) {
			while (parser.nextToken() != JSONToken.END_OBJECT) {
				if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
					String text = parser.getText();
					if (parser.nextToken() != JSONToken.VALUE_NULL) {
						if (text == 'givenName') {
							givenName = parser.getText();
						} else if (text == 'familyName') {
							familyName = parser.getText();
						} else if (text == 'formatted') {
							formatted = parser.getText();
						} else {
							System.debug(LoggingLevel.WARN, 'Name consuming unrecognized property: '+text);
							consumeObject(parser);
						}
					}
				}
			}
		}
	}
	
	public class Photos {
		public String value {get;set;}

		public Photos(JSONParser parser) {
			while (parser.nextToken() != JSONToken.END_OBJECT) {
				if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
					String text = parser.getText();
					if (parser.nextToken() != JSONToken.VALUE_NULL) {
						if (text == 'value') {
							value = parser.getText();
						} else {
							System.debug(LoggingLevel.WARN, 'Photos consuming unrecognized property: '+text);
							consumeObject(parser);
						}
					}
				}
			}
		}
	}
	
	public class Emails {
		public String value {get;set;}
		public String type {get;set;}
		public String jive_label {get;set;}
		public Boolean primary {get;set;}

		public Emails(JSONParser parser) {
			while (parser.nextToken() != JSONToken.END_OBJECT) {
				if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
					String text = parser.getText();
					if (parser.nextToken() != JSONToken.VALUE_NULL) {
						if (text == 'value') {
							value = parser.getText();
						} else if (text == 'type') {
							type = parser.getText();
						} else if (text == 'jive_label') {
							jive_label = parser.getText();
						} else if (text == 'primary') {
							primary = parser.getBooleanValue();
						} else {
							System.debug(LoggingLevel.WARN, 'Emails consuming unrecognized property: '+text);
							consumeObject(parser);
						}
					}
				}
			}
		}
	}
	
	public class Jive {
		public Level level {get;set;}
		public String timeZone {get;set;}
		public String locale {get;set;}
		public String username {get;set;}
		public List<Profile> profile {get;set;}
		public Boolean external {get;set;}
		public Boolean visible {get;set;}
		public Boolean enabled {get;set;}
		public Boolean externalContributor {get;set;}
		public Boolean federated {get;set;}

		public Jive(JSONParser parser) {
			while (parser.nextToken() != JSONToken.END_OBJECT) {
				if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
					String text = parser.getText();
					if (parser.nextToken() != JSONToken.VALUE_NULL) {
						if (text == 'level') {
							level = new Level(parser);
						} else if (text == 'timeZone') {
							timeZone = parser.getText();
						} else if (text == 'locale') {
							locale = parser.getText();
						} else if (text == 'username') {
							username = parser.getText();
						} else if (text == 'profile') {
							profile = new List<Profile>();
							while (parser.nextToken() != JSONToken.END_ARRAY) {
								profile.add(new Profile(parser));
							}
						} else if (text == 'external') {
							external = parser.getBooleanValue();
						} else if (text == 'visible') {
							visible = parser.getBooleanValue();
						} else if (text == 'enabled') {
							enabled = parser.getBooleanValue();
						} else if (text == 'externalContributor') {
							externalContributor = parser.getBooleanValue();
						} else if (text == 'federated') {
							federated = parser.getBooleanValue();
						} else {
							System.debug(LoggingLevel.WARN, 'Jive consuming unrecognized property: '+text);
							consumeObject(parser);
						}
					}
				}
			}
		}
	}
	
	public Name name {get;set;}
	public String location {get;set;}
	public String type {get;set;}
	public String displayName {get;set;}
	public String status {get;set;}
	public List<String> tags {get;set;}
	public String published {get;set;}
	public Integer followingCount {get;set;}
	public String thumbnailUrl {get;set;}
	public Jive jive {get;set;}
	public String updated {get;set;}
	public List<Emails> emails {get;set;}
	public List<Photos> photos {get;set;}
	public String thumbnailId {get;set;}
	public Integer followerCount {get;set;}
	public Resources resources {get;set;}
	public String id {get;set;}

	public JSON2Apex(JSONParser parser) {
		while (parser.nextToken() != JSONToken.END_OBJECT) {
			if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
				String text = parser.getText();
				if (parser.nextToken() != JSONToken.VALUE_NULL) {
					if (text == 'name') {
						name = new Name(parser);
					} else if (text == 'location') {
						location = parser.getText();
					} else if (text == 'type') {
						type = parser.getText();
					} else if (text == 'displayName') {
						displayName = parser.getText();
					} else if (text == 'status') {
						status = parser.getText();
					} else if (text == 'tags') {
						tags = new List<String>();
						while (parser.nextToken() != JSONToken.END_ARRAY) {
							tags.add(new String(parser));
						}
					} else if (text == 'published') {
						published = parser.getText();
					} else if (text == 'followingCount') {
						followingCount = parser.getIntegerValue();
					} else if (text == 'thumbnailUrl') {
						thumbnailUrl = parser.getText();
					} else if (text == 'jive') {
						jive = new Jive(parser);
					} else if (text == 'updated') {
						updated = parser.getText();
					} else if (text == 'emails') {
						emails = new List<Emails>();
						while (parser.nextToken() != JSONToken.END_ARRAY) {
							emails.add(new Emails(parser));
						}
					} else if (text == 'photos') {
						photos = new List<Photos>();
						while (parser.nextToken() != JSONToken.END_ARRAY) {
							photos.add(new Photos(parser));
						}
					} else if (text == 'thumbnailId') {
						thumbnailId = parser.getText();
					} else if (text == 'followerCount') {
						followerCount = parser.getIntegerValue();
					} else if (text == 'resources') {
						resources = new Resources(parser);
					} else if (text == 'id') {
						id = parser.getText();
					} else {
						System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
						consumeObject(parser);
					}
				}
			}
		}
	}
	
	public class Following {
		public String ref {get;set;}
		public List<String> allowed {get;set;}

		public Following(JSONParser parser) {
			while (parser.nextToken() != JSONToken.END_OBJECT) {
				if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
					String text = parser.getText();
					if (parser.nextToken() != JSONToken.VALUE_NULL) {
						if (text == 'ref') {
							ref = parser.getText();
						} else if (text == 'allowed') {
							allowed = new List<String>();
							while (parser.nextToken() != JSONToken.END_ARRAY) {
								allowed.add(new String(parser));
							}
						} else {
							System.debug(LoggingLevel.WARN, 'Following consuming unrecognized property: '+text);
							consumeObject(parser);
						}
					}
				}
			}
		}
	}
	
	public class Profile {
		public String value {get;set;}
		public String jive_label {get;set;}

		public Profile(JSONParser parser) {
			while (parser.nextToken() != JSONToken.END_OBJECT) {
				if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
					String text = parser.getText();
					if (parser.nextToken() != JSONToken.VALUE_NULL) {
						if (text == 'value') {
							value = parser.getText();
						} else if (text == 'jive_label') {
							jive_label = parser.getText();
						} else {
							System.debug(LoggingLevel.WARN, 'Profile consuming unrecognized property: '+text);
							consumeObject(parser);
						}
					}
				}
			}
		}
	}
	
	public class Level {
		public String name {get;set;}
		public String description {get;set;}
		public Integer points {get;set;}

		public Level(JSONParser parser) {
			while (parser.nextToken() != JSONToken.END_OBJECT) {
				if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
					String text = parser.getText();
					if (parser.nextToken() != JSONToken.VALUE_NULL) {
						if (text == 'name') {
							name = parser.getText();
						} else if (text == 'description') {
							description = parser.getText();
						} else if (text == 'points') {
							points = parser.getIntegerValue();
						} else {
							System.debug(LoggingLevel.WARN, 'Level consuming unrecognized property: '+text);
							consumeObject(parser);
						}
					}
				}
			}
		}
	}
	
	public class Resources {
		public Following following {get;set;}
		public Following followers {get;set;}
		public Following activity {get;set;}

		public Resources(JSONParser parser) {
			while (parser.nextToken() != JSONToken.END_OBJECT) {
				if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
					String text = parser.getText();
					if (parser.nextToken() != JSONToken.VALUE_NULL) {
						if (text == 'following') {
							following = new Following(parser);
						} else if (text == 'followers') {
							followers = new Following(parser);
						} else if (text == 'activity') {
							activity = new Following(parser);
						} else {
							System.debug(LoggingLevel.WARN, 'Resources consuming unrecognized property: '+text);
							consumeObject(parser);
						}
					}
				}
			}
		}
	}
	
	
	public static JSON2Apex parse(String json) {
		return new JSON2Apex(System.JSON.createParser(json));
	}
	
	// This test method should give 100% coverage
	static testMethod void testParse() {
		String json = '{'+
		'  \"name\" : {'+
		'    \"givenName\" : \"smith\",'+
		'    \"familyName\" : \"peter\",'+
		'    \"formatted\" : \"Smith Peter\"'+
		'  },'+
		'  \"location\" : \"london\",'+
		'  \"type\" : \"person\",'+
		'  \"displayName\" : \"smith peter\",'+
		'  \"status\" : \"xyz gets recognized by industry analyst as a leader...\",'+
		'  \"tags\" : [ \"leadership\", \"financial\", \"analysis\", \"capital\", \"formation\", \"contract\", \"talent\", \"management\", \"entrepreneur\" ],'+
		'  \"published\" : \"2011-11-01T20:17:08.667+0000\",'+
		'  \"followingCount\" : 64,'+
		'  \"thumbnailUrl\" : \"https://xyz.com\",'+
		'  \"jive\" : {'+
		'    \"level\" : {'+
		'      \"name\" : \"Contributor\",'+
		'      \"description\" : \"\",'+
		'      \"points\" : 65'+
		'    },'+
		'    \"timeZone\" : \"America/New_York\",'+
		'    \"locale\" : \"en_US\",'+
		'    \"username\" : \"psmith\",'+
		'    \"profile\" : [ {'+
		'      \"value\" : \"President & CEO\",'+
		'      \"jive_label\" : \"Title\"'+
		'    }, {'+
		'      \"value\" : \"11/06/2011\",'+
		'      \"jive_label\" : \"Join Date\"'+
		'    }, {'+
		'      \"value\" : \"lived in NY \",'+
		'      \"jive_label\" : \"Biography\"'+
		'    }, {'+
		'      \"value\" : \"\",'+
		'      \"jive_label\" : \"Expertise\"'+
		'    }, {'+
		'      \"value\" : [ \"Financial Services \", \"Business Services \" ],'+
		'      \"jive_label\" : \"Industries\"'+
		'    }, {'+
		'      \"value\" : [ \"Finance \", \"Business Development \", \"Consulting \", \"Business Analysis \", \"Marketing \" ],'+
		'      \"jive_label\" : \"Disciplines\"'+
		'    }, {'+
		'      \"value\" : \".\",'+
		'      \"jive_label\" : \"What do you do?\"'+
		'    }, {'+
		'      \"value\" : \"team.\",'+
		'      \"jive_label\" : \"My Style\"'+
		'    }, {'+
		'      \"value\" : \"http://www.xyz.com\",'+
		'      \"jive_label\" : \"Website\"'+
		'    }, {'+
		'      \"value\" : \"\",'+
		'      \"jive_label\" : \"Business Manager\"'+
		'    }, {'+
		'      \"value\" : \"https://twitter.com/#!/x\",'+
		'      \"jive_label\" : \"Twitter\"'+
		'    } ],'+
		'    \"external\" : false,'+
		'    \"visible\" : true,'+
		'    \"enabled\" : true,'+
		'    \"externalContributor\" : false,'+
		'    \"federated\" : false'+
		'  },'+
		'  \"updated\" : \"2013-11-12T04:57:20.140+0000\",'+
		'  \"emails\" : [ {'+
		'    \"value\" : \"xyz.com@localhost\",'+
		'    \"type\" : \"work\",'+
		'    \"jive_label\" : \"Email\",'+
		'    \"primary\" : true'+
		'  } ],'+
		'  \"photos\" : [ {'+
		'    \"value\" : \"https://xy.com\"'+
		'  } ],'+
		'  \"thumbnailId\" : \"1093\",'+
		'  \"followerCount\" : 50,'+
		'  \"resources\" : {'+
		'    \"following\" : {'+
		'      \"ref\" : \"xy.xom\",'+
		'      \"allowed\" : [ \"GET\" ]'+
		'    },'+
		'    \"followers\" : {'+
		'      \"ref\" : \"xy.com\",'+
		'      \"allowed\" : [ \"GET\" ]'+
		'    },'+
		'    '+
		'    \"activity\" : {'+
		'      \"ref\" : \"https://xys.com\",'+
		'      \"allowed\" : [ \"GET\" ]'+
		'    }'+
		'  },'+
		'  \"id\" : \"2010\"'+
		'}';
		JSON2Apex r = parse(json);
		System.assert(r != null);

		json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
		Name objName = new Name(System.JSON.createParser(json));
		System.assert(objName != null);
		System.assert(objName.givenName == null);
		System.assert(objName.familyName == null);
		System.assert(objName.formatted == null);

		json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
		Photos objPhotos = new Photos(System.JSON.createParser(json));
		System.assert(objPhotos != null);
		System.assert(objPhotos.value == null);

		json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
		Emails objEmails = new Emails(System.JSON.createParser(json));
		System.assert(objEmails != null);
		System.assert(objEmails.value == null);
		System.assert(objEmails.type == null);
		System.assert(objEmails.jive_label == null);
		System.assert(objEmails.primary == null);

		json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
		Jive objJive = new Jive(System.JSON.createParser(json));
		System.assert(objJive != null);
		System.assert(objJive.level == null);
		System.assert(objJive.timeZone == null);
		System.assert(objJive.locale == null);
		System.assert(objJive.username == null);
		System.assert(objJive.profile == null);
		System.assert(objJive.external == null);
		System.assert(objJive.visible == null);
		System.assert(objJive.enabled == null);
		System.assert(objJive.externalContributor == null);
		System.assert(objJive.federated == null);

		json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
		JSON2Apex objRoot = new JSON2Apex(System.JSON.createParser(json));
		System.assert(objRoot != null);
		System.assert(objRoot.name == null);
		System.assert(objRoot.location == null);
		System.assert(objRoot.type == null);
		System.assert(objRoot.displayName == null);
		System.assert(objRoot.status == null);
		System.assert(objRoot.tags == null);
		System.assert(objRoot.published == null);
		System.assert(objRoot.followingCount == null);
		System.assert(objRoot.thumbnailUrl == null);
		System.assert(objRoot.jive == null);
		System.assert(objRoot.updated == null);
		System.assert(objRoot.emails == null);
		System.assert(objRoot.photos == null);
		System.assert(objRoot.thumbnailId == null);
		System.assert(objRoot.followerCount == null);
		System.assert(objRoot.resources == null);
		System.assert(objRoot.id == null);

		json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
		Following objFollowing = new Following(System.JSON.createParser(json));
		System.assert(objFollowing != null);
		System.assert(objFollowing.ref == null);
		System.assert(objFollowing.allowed == null);

		json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
		Profile objProfile = new Profile(System.JSON.createParser(json));
		System.assert(objProfile != null);
		System.assert(objProfile.value == null);
		System.assert(objProfile.jive_label == null);

		json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
		Level objLevel = new Level(System.JSON.createParser(json));
		System.assert(objLevel != null);
		System.assert(objLevel.name == null);
		System.assert(objLevel.description == null);
		System.assert(objLevel.points == null);

		json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
		Resources objResources = new Resources(System.JSON.createParser(json));
		System.assert(objResources != null);
		System.assert(objResources.following == null);
		System.assert(objResources.followers == null);
		System.assert(objResources.activity == null);
	}
}

 

 

 

AaranAaran

Hi,

 

Is there are any way to handle string value and list<string> value under the same class.  Here I have to create a profile class which has string value and list<string> value for jive_label.  Can someone help me to parse these variables pls?

 

{
"name" : {
"givenName" : "smith",
"familyName" : "peter",
"formatted" : "Smith Peter"
},
"location" : "london",
"type" : "person",
"displayName" : "smith peter",
"tags" : [ "leadership", "financial", "analysis", "capital", "formation", "contract", "talent", "management", "entrepreneur" ],
"jive" : {
"level" : {
"name" : "Contributor",
"description" : "",
"points" : 65
},
"timeZone" : "America/New_York",
"locale" : "en_US",
"username" : "psmith",
"profile" : [ {
"value" : "President & CEO",
"jive_label" : "Title"
}, {
"value" : "yes",
"jive_label" : "Expertise"
}, {
"value" : [ "Financial Services ", "Business Services " ],
"jive_label" : "Industries"
}, {
"value" : [ "Finance ", "Business Development ", "Consulting ", "Business Analysis ", "Marketing " ],
"jive_label" : "Disciplines"
}, {
"value" : ".",
"jive_label" : "What do you do?"
}, {
"value" : "http://www.facebook.com/x",
"jive_label" : "Facebook"
} ],
"federated" : false
},
"id" : "2010"
}

 

 

SuperfellSuperfell

Try not using the "generate explict parse code" option.