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

Parsing unpredictable JSON

I am trying to parse JSON that has an element that can come over as either a string or list... 

"people": [
{
	"jobFunction": "Marketing",
	"jobLevel": "C-Level",
	"industry": "Software"
},{
	"jobFunction": ["Marketing","Sales"],
	"jobLevel": "C-Level",
	"industry": "Software"
}]
I can't figure out how to parse JSON that contains both such as above. 

I had been trying to parse using a custom class (JSON 2 APEX), but it breaks when there are both found in a single return... any ideas? 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
Hi, this is the correct format
{
"people":[

{
	"jobFunction": "Marketing",
	"jobLevel": "C-Level",
	"industry": "Software"
},
{
	"jobFunction": ["Marketing","Sales"],
	"jobLevel": "C-Level",
	"industry": "Software"
}
]
}

Please mark my answer as best, if it works
Justin Kitagawa 6Justin Kitagawa 6
The JSON itself was just an example. The issue is how to parse the JSON when an element can be returned as either a string or a list. Using a concrete class like JSON2APEX causes an error if both list and string are returned at the same time. 

 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Try this 

http://json2apex.herokuapp.com/

Amit Chaudhary 8Amit Chaudhary 8
Sample JSON for you
{
"people":[

{
	"jobFunction": "Marketing",
	"jobLevel": "C-Level",
	"industry": "Software"
},
{
	"jobFunction": ["Marketing","Sales"],
	"jobLevel": "C-Level",
	"industry": "Software"
}
]
}
JSON to APEX
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

	public List<People> people;

	public class People {
		public String jobFunction;
		public String jobLevel;
		public String industry;
	}

	
	public static JSON2Apex parse(String json) {
		return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
	}
}
TEst Class.
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

@IsTest
public class JSON2Apex_Test {
	
	static testMethod void testParse() {
		String json = '{'+
		'\"people\":['+
		''+
		'{'+
		'	\"jobFunction\": \"Marketing\",'+
		'	\"jobLevel\": \"C-Level\",'+
		'	\"industry\": \"Software\"'+
		'},'+
		'{'+
		'	\"jobFunction\": [\"Marketing\",\"Sales\"],'+
		'	\"jobLevel\": \"C-Level\",'+
		'	\"industry\": \"Software\"'+
		'}'+
		']'+
		'}';
		JSON2Apex obj = JSON2Apex.parse(json);
		System.assert(obj != null);
	}
}
Let us know if this will help you

Thanks
Amit Chaudhary


 
SKolakanSKolakan
Justin,
Use the following method to convert node to array when you get only one element in some cases. 
 
public static string convertNodeAsArray(String jsonString,string nodeName){
    Pattern MyPattern = Pattern.compile(nodeName+'":"(.*?)"');
    Matcher MyMatcher = MyPattern.matcher(jsonString);
    String convertedJSON = '';
    
    while(MyMatcher.find()){
        convertedJSON = MyMatcher.replaceFirst(nodeName+'":["'+MyMatcher.group(1)+'"]');
        MyMatcher = MyPattern.matcher(convertedJSON);
    }    
    return convertedJSON;    
}

Hope that helps!