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
Justin Kitagawa 11Justin Kitagawa 11 

JSON Parser not finding data...

I have a class that does a callout to which returns JSON like this: 
{
	"personMatchResponse": {
		"inputParameters": {
			"parameter": {
				"name": "outputFieldOptions",
				"value": "jobFunction,managementLevel"
			}
		},
		"matchResults": {
			"personMatchResults": {
				"personMatchResult": {
					"matchPersonInput": {
						"uniqueInputId": "Zoom-1"
					},
					"personMatches": {
						"personMatch": {
							"firstName": "First",
							"lastName": "Last",
							"phone": "number",
							"currentEmployment": {
								"jobTitle": "Title",
								"jobFunction": "Function",
								"managementLevel": "C-Level",
								"company": {
									"companyId": 002400,
								}
							},
							"industry": ["Software Development & Design", "Software", "Engineering Software"]
						}
					}
				}
			}
		}
	}
}

and my code to parse is: 
//Parse JSON 
        JSONParser parser = JSON.createParser(res.getbody());
        System.JSONToken token;
        string text;

            parser.nextToken();     // 
            parser.nextToken();     // 
            parser.nextToken();     //
            parser.nextToken();     //

        while((token = parser.nextToken()) != null) {
            // Parse the object
            if ((token = parser.getCurrentToken()) != JSONToken.END_OBJECT) {
                text = parser.getText();
                if (token == JSONToken.FIELD_Name && text == 'personId' ) {
                    token=parser.nextToken();
                    thisLead.Zoom_Info_ID__c = parser.getText();
                    thisLead.zisf__zoom_id__c = parser.getText();
                    
                
                } else if (token == JSONToken.FIELD_Name && text == 'companyId') {
                    token=parser.nextToken();
                    thisLead.Zoom_Info_Company_ID__c = parser.getText();
                    
                
                }  else if (token == JSONToken.FIELD_Name && text == 'phone') {
                    token=parser.nextToken();
                    thisLead.Zoom_Info_Direct_Line__c = parser.getText();
                    
                
                }  else if (token == JSONToken.FIELD_Name && text == 'jobTitle') {
                    token=parser.nextToken();
                    thisLead.title = parser.getText();
                    
                
                } 
            } 
                                    
                // advance to next object
                //token = parser.nextToken();
                if (token == JSONToken.END_ARRAY) {             // we reached end of array 
                        break;
                }
            }


It was working fine before I added something to the request that caused the "inputParameters": { section to be returned. Is there a way to make it skip that section and just go the match results? 

Help is greatly appreciated! 

Amit Chaudhary 8Amit Chaudhary 8
Please try below tool to convert JSOn o Apex
1) https://www.adminbooster.com/tool/json2apex
Your Apex class should be like that
//
//Generated by AdminBooster
//

public class fromJSON{
	public cls_personMatchResponse personMatchResponse;
	class cls_personMatchResponse {
		public cls_inputParameters inputParameters;
		public cls_matchResults matchResults;
	}
	class cls_inputParameters {
		public cls_parameter parameter;
	}
	class cls_parameter {
		public String name;	//outputFieldOptions
		public String value;	//jobFunction,managementLevel
	}
	class cls_matchResults {
		public cls_personMatchResults personMatchResults;
	}
	class cls_personMatchResults {
		public cls_personMatchResult personMatchResult;
	}
	class cls_personMatchResult {
		public cls_matchPersonInput matchPersonInput;
		public cls_personMatches personMatches;
	}
	class cls_matchPersonInput {
		public String uniqueInputId;	//Zoom-1
	}
	class cls_personMatches {
		public cls_personMatch personMatch;
	}
	class cls_personMatch {
		public String firstName;	//First
		public String lastName;	//Last
		public String phone;	//number
		public cls_currentEmployment currentEmployment;
		public cls_industry[] industry;
	}
	class cls_currentEmployment {
		public String jobTitle;	//Title
		public String jobFunction;	//Function
		public String managementLevel;	//C-Level
		public cls_company company;
	}
	class cls_company {
		public Integer companyId;	//1280
	}
	class cls_industry {
		public String Name;	//Software Development & Design
	}
	public static fromJSON parse(String json){
		return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
	}

	static testMethod void testParse() {
		String json=		'{'+
		'	"personMatchResponse": {'+
		'		"inputParameters": {'+
		'			"parameter": {'+
		'				"name": "outputFieldOptions",'+
		'				"value": "jobFunction,managementLevel"'+
		'			}'+
		'		},'+
		'		"matchResults": {'+
		'			"personMatchResults": {'+
		'				"personMatchResult": {'+
		'					"matchPersonInput": {'+
		'						"uniqueInputId": "Zoom-1"'+
		'					},'+
		'					"personMatches": {'+
		'						"personMatch": {'+
		'							"firstName": "First",'+
		'							"lastName": "Last",'+
		'							"phone": "number",'+
		'							"currentEmployment": {'+
		'								"jobTitle": "Title",'+
		'								"jobFunction": "Function",'+
		'								"managementLevel": "C-Level",'+
		'								"company": {'+
		'									"companyId": 002400,'+
		'								}'+
		'							},'+
		'							"industry": [{"Name":"Software Development & Design"} ,{"Name":"Software"}]'+
		'						}'+
		'					}'+
		'				}'+
		'			}'+
		'		}'+
		'	}'+
		'}';
		fromJSON obj = parse(json);
		System.assert(obj != null);
	}
}
NOTE:- minar change i did in your JSOn
"industry": [{"Name":"Software Development & Design"} ,{"Name":"Software"}]



 
Justin Kitagawa 11Justin Kitagawa 11

Thanks Amit, 
 

But I am still a bit confused how to use the above class to parse the JSON, can you provide a bit more detail? It would be greatly  appreciated! 

 

Justin Kitagawa 11Justin Kitagawa 11
I am now getting this back as a fromJSON class 
[personMatchResponse=cls_personMatchResponse:[matchResults=cls_matchResults:[personMatchResults=cls_personMatchResults:[personMatchResult=cls_personMatchResult:[personMatches=cls_personMatches:[personMatch=cls_personMatch:[currentEmployment=cls_currentEmployment:[company=cls_company:[companyId=355111921907, companyName=Company, companyPhone=(111) 111-1111], jobFunction=Finance, jobTitle=Chief Officer, managementLevel=C-Level], fax=(111) 111-1111, firstName=First, lastName=Last, matchConfidence=99.99998823529411, personId=96454303, phone=(111) 111-1111]]]]]]]

but am unsure how to pull data from that