Wednesday, 1 February 2017

Json utils

As part of api testing, we might need to play with json objects, arrays and key-value pairs.

here is the solution.


Maven dependency:

<!-- Rest Utils Returns JsonObject :: http://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>

Java code:



import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

/**
 * Class which consists of differnt methods related to Json Utils
 */
public class JsonUtils {

/**
* Method to Converts given String to JSON object
*
* @param str
* @return json Object
*/
public static JSONObject getJsonObjectFromString(String str) {
return new JSONObject(str);
}

/**
* Method to Converts given String to JSON Array
*
* @param str
* @return json array
*/
public static JSONArray getJsonArrayFromString(String str) {
return new JSONArray(str);
}

/**
* method to read json object form resp object
*
* @param response
* @return json object
*/
public static JSONObject getJsonFromResponse(HttpResponse response) {
String responseBody = null;
try {
responseBody = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObj = new JSONObject(responseBody);
return jsonObj;
}

/**
* method to read json array form resp object
* @param response
* @return JsonArray
* @throws JSONException
*/
public static JSONArray getJsonArrayFromResponse(HttpResponse response) {
String responseBody = null;
try {
responseBody = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
JSONArray jsonObj = new JSONArray(responseBody);
return jsonObj;
}

/**
* Method to read value from json for the said key
*
* @param jsonObj
* @param key
* @return
*/
public static Object getValueFromJson(JSONObject jsonObj, String key) {
Object propertyUtilityCount = 0;
propertyUtilityCount = jsonObj.get(key);
return propertyUtilityCount;
}

/**
* Method to read json array from json object for the said key
*
* @param jsonObj
* @param key
* @return
*/
public static JSONArray getSaidJSonArrayFromJson(JSONObject jsonObj, String key) {
Object object = jsonObj.getJSONArray(key);
if (object instanceof JSONArray) {
return (JSONArray) object;
} else {
throw new JSONException(key + " JSONObject is not a JSONArray.");
}
}

/**
* Fetch the object with in an array and through index we fetch which Object
* we want to fetch
*
* @param getResponse
* @param i
* @return
* @throws IOException
*/
public static JSONObject getJsonArrayFromJsonObject(HttpResponse getResponse, int i) {

String result = "";
try {
result = EntityUtils.toString(getResponse.getEntity());
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
JSONArray e = new JSONArray(result);
JSONObject obj = e.getJSONObject(i);
return obj;
}

/**
* Method to fetch json object from json array for the said index
*
* @param jsonArray
* @param i
* @return json object
*/
public static JSONObject getSaidJsonObjectFromJsonArray(JSONArray jsonArray, int i) {
JSONObject obj = new JSONObject();
obj = jsonArray.getJSONObject(i);
return obj;
}

/**
* Method to fetch json object from json object for the said key
*
* @param jsonObj
* @param key
* @return json object
*/
public static JSONObject getSaidJsonObjectFromJsonObject(JSONObject jsonObj, String key) {
JSONObject obj = new JSONObject();
obj = jsonObj.getJSONObject(key);
return obj;
}

/**
* Method to fetch json object from json object
*
* @param jsonObj
* @return json object
*/
public static JSONObject getSaidJsonObjectFromJsonObject(JSONObject jsonObj) {
JSONObject obj = new JSONObject();
return obj;
}

/**
* verifies that given json is empty or not
*
* @param json
*            obj
* @return boolean value
*/
public static boolean isNonEmpty(JSONObject obj) {
boolean result = false;
if (obj.length() <= 0 || obj != null) {
result = true;
}
return result;
}

/**
* checks the given json array is empty or not
*
* @param obj
* @return boolean value
*/
public static boolean isNonEmpty(JSONArray obj) {
boolean result = false;
if (obj.length() <= 0 || obj != null) {
result = true;
}
return result;
}

/**
* converts json object to xml
*
* @param jsonObj
* @return String
*/
public String toXML(JSONObject jsonObj) {
String xml = XML.toString(jsonObj);
return xml;
}

}

No comments:

Post a Comment