Cisco call studio : Java code for creating custom element which parses JSON

by sunil ravulapalli /5. November 2013 08:07 /cisco-voice-programming /Comments (0)
import com.audium.server.AudiumException;
import com.audium.server.voiceElement.ActionElementBase;
import com.audium.server.voiceElement.ElementInterface;
import com.audium.server.voiceElement.Setting;
import com.audium.server.voiceElement.ElementData;
import com.audium.server.voiceElement.ElementException;
import com.audium.server.xml.ActionElementConfig;
import com.audium.server.session.ActionElementData;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class GetFieldFromJSONElement extends ActionElementBase implements ElementInterface 
{
    public String getElementName()
    {
        return "GetFieldFromJSON";
    }

    public String getDisplayFolderName()
    {
        return "Company Elements";
    }

    public String getDescription() 
    {
        return "This element gets the value of the required field in a JSON string";
    }
    
	public Setting[] getSettings() throws ElementException 
	 {	
		 Setting[] settingArray = new Setting[4];
		 
		 settingArray[0] = new Setting("json", 
				 						"JSON", 
						   				"This is the input JSON",
						   				true,
						   				true,
						   				true,
						   				Setting.STRING);
		 
		 settingArray[1] = new Setting("field", 
					"Field", 
	   				"This is the input JSON",
	   				true,
	   				true,
	   				true,
	   				Setting.STRING);
		 
		 settingArray[2] = new Setting("resultType", 
				 						"Result Type", 
				   						"Choose where to store result \n" +
				   						"into Element or Session data",
				   						true,
				   						true,
				   						false,
				   						new String[]{"Element","Session"});
		settingArray[2].setDefaultValue("Session");		
		
		settingArray[3] = new Setting("resultName", 
										"Result Name", 
										"Name of variable to hold the result.",
										true,
										true,
										true,
										Setting.STRING);	
		settingArray[3].setDefaultValue("resultString");

		return settingArray;
	 }

    public ElementData[] getElementData() throws ElementException 
    {
        return null;
    }
    
	public void	doAction(String name, ActionElementData actionData) throws AudiumException
	{
		ActionElementConfig config = actionData.getActionElementConfig();			
		String json = config.getSettingValue("json",actionData);
		String field = config.getSettingValue("field",actionData);
		String resultType = config.getSettingValue("resultType",actionData);
		String resultName = config.getSettingValue("resultName",actionData);
		
		try {						
			String value = GetValueOfField(json, field);				
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,value);
			} else {
				actionData.setSessionData(resultName,value);
			} 
			actionData.setElementData("status","success");
		} catch (Exception e) {
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,e.toString());
			} else {
				actionData.setSessionData(resultName,e.toString());
			}
			
			actionData.setElementData("status","failure");
		}		
	}

	public String GetValueOfField(String json, String field) throws Exception
	{   
		JSONParser parser = new JSONParser();
		json = json.replaceAll("'", "\"");
		
		JSONObject jsonObject =  (JSONObject) parser.parse(json);
		
		Object val = jsonObject.get(field);
	    
		return val.toString();
	}
}
blog comments powered by Disqus