Compare JSON response using JUnit and JSONassert - java

I'm relatively new to Java and I'm asking to write test of JSON response server.
I found JSONassert very useful but I didn't succeed to write the method getRESTData.
Anybody can help please?
#Test
public void testGetFriends() throws JSONException {
JSONObject data = getRESTData("/friends/367.json");
String expected = "{friends:[{id:123,name:\"Corby Page\"}"
+ ",{id:456,name:\"Solomon Duskis\"}]}";
JSONAssert.assertEquals(expected, data, false);
}

You can get the data as String and pass it into JSONAssert.assertEquals.
Converting to JSONObject isn't necessary.
To fetch data from an URL, you can use URL.getContent method:
final String data = new URL("...").getContent();
String expected = "{friends:[{id:123,name:\"Corby Page\"}"
+ ",{id:456,name:\"Solomon Duskis\"}]}";
JSONAssert.assertEquals(expected, data, false);

This can also be achieved with ModelAssert - https://github.com/webcompere/model-assert , which can take anything serializable to JSON as an input:
#Test
public void testGetFriends() throws JSONException {
JSONObject data = getRESTData("/friends/367.json");
String expected = "{friends:[{id:123,name:\"Corby Page\"}"
+ ",{id:456,name:\"Solomon Duskis\"}]}";
assertJson(data).isEqualTo(expected);
}
IIRC JSONObject is essentially a Map, so assertJson will convert it to the internal JsonNode format it uses for comparison.

Related

GET JSON response, parse it and then convert back to a JSON

My goal is;
GET a JSON file from an API and make changes to the data
Then generate a new dictionary from this data
Convert this dictionary into JSON
So far I have points 1 and 2 complete but struggling to understand how I can convert the changes into a new JSON
Main.java
//get JSON from API
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://jsonplaceholder.typicode.com/albums")).build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
//.thenAccept(System.out::println)
.thenApply(Main::parse)
.join();
}
//Parse Json
public static String parse(String responseBody) {
JSONArray albums = new JSONArray(responseBody);
for(int i = 0 ; i < albums.length(); i++) {
JSONObject album = albums.getJSONObject(i);
int id = album.getInt("id");
int userId = album.getInt("userId");
String title = album.getString("title");
System.out.println("id: " + id);
System.out.println("userId: " + userId);
System.out.println("title: " + title);
}
return null;
}
}
Thanks in advance
It depends on the library that contains JSONArray and JSONObject, but I've seen quite a few libraries where the toString() method returns the JSON you're looking for.
org.json is a simple library. It is possible to do what you want with this library but I can offer you much simpler solution. Your code may look something like this:
public static String parse(String responseBody) {
List<Map<String, Object>> myList = JsonUtils.readObjectFromJsonString(responseBody, List.class); //here you get a list of Maps that contasins your data
//Here you modify your data
return JsonUtils.writeObjectToJsonString(myList); //Here you convert your modified list of maps mack to Json String
}
You will need to get MgntUtils library to use JsonUtils class. You can get the library as maven artifact here or as just a jar file here. Here is Javadoc for JsonUtils class

Obtain specific part of a string with JSON Format java

I have a question on how would be the best way to get the information from a string but that has JSON format.
{
"internal_id":"1234",
"moreInformation":"Failed authentication for user."
}
In this case, I want to get the value of "internal_id" and I already did, with subtring, lastIndexOf and indexOf
public static String returnInternalCode(String json){
String internalCode = json.substring(json.lastIndexOf("\"internal_id\":\"") + "\"internal_id\":\"".length(), json.length() - 1);
if (json.lastIndexOf("\"internal_id\":\"") == -1) return null;
return internalCode.substring(0, internalCode.indexOf("\""));
}
I also tried several JSONs with order changes that don't have the data and it also worked. I leave the full class of tests I did:
public class Test {
public static void main(String[] args) {
// Original JSON
String json = "{\"internal_id\":\"999999\",\"moreInformation\":\"Failed authentication for user, 1 authentication attempt remaining.\"}";
// Other JSON order
String json2 = "{\"moreInformation\":\"Failed authentication for user. Invalid response.\", \"moreInformation2\":\"Failed authentication for user. \", \"internal_id\":\"45678\"}";
// JSON without the internal_id
String json3 = "{\"moreInformation\":\"Failed authentication for user. Invalid response.\"}";
// JSON without moreInformation
String json4 = "{\"internal_id\":\"999999\"}";
System.out.println("JSON: ".concat(json4));
System.out.println("internalId: " + returnInternalId(json4));
System.out.println("moreInformation: " + returnMoreInformation(json4));
}
public static String returnInternalId(String json){
String internalCode = json.substring(json.lastIndexOf("\"internal_id\":\"") + "\"internal_id\":\"".length(), json.length());
if (json.lastIndexOf("\"internal_id\":\"") == -1) return null;
return internalCode.substring(0, internalCode.indexOf("\""));
}
public static String returnMoreInformation(String json){
String moreInformation = (json.substring(json.lastIndexOf("\"moreInformation\":\"") + "\"moreInformation\":\"".length(), json.length()));
if (json.lastIndexOf("\"moreInformation\":\"") == -1) return null;
return moreInformation.substring(0, moreInformation.indexOf("\""));
}
}
I would like to know if there are better ways to do what I did, such as with StringBuilder or StringBuffer and also to find out which way uses less memory or is faster to run, how do I know that? How long does it take to execute a method?
Thank you very much!
You can extract the values this way; Using Simple-json library
JSONObject jobj = (JSONObject) parser.parse(yourJsonString); // Pass the Json formatted String
String internal_id = (String) jobj.get("internal_id"); // Extract the value from your key
System.out.println(internal_id); // 1234

How to convert a JSON String to a JSON Object (Gson) in Java?

I am consuming an external API to which the response body is a String in JSON format. I am using Gson to parse the String into a JSON Object. However, I get the following error when trying to display the object in browser.
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of
the JSON data
Here is my code:
Controller
#RequestMapping("/player")
public JsonArray displayPlayer() throws Exception{
String player = "PacSnackz";
smite.createSession();
String test = smite.getPlayer(player);
JsonArray playerObj = new JsonParser().parse(test).getAsJsonArray();
return playerObj;
}
Smite Class
public String getPlayer(String player) throws Exception {
if (!isSessionValid() && !createSession()) return "session null";
return getURL(combine(new String[] {
baseURL + "getplayer" + responseFormat,
devID,
getSignature("getplayer"),
sessionID,
getTimestamp(),
player
}, "/"));
}
This is what the JSON String looks like:
JSON String
[{"Avatar_URL":"","Created_Datetime":"8\/16\/2016 4:30:14 AM","Id":9993055,"Last_Login_Datetime":"4\/23\/2018 8:47:56 PM","Leaves":3,"Level":17,"Losses":19,"MasteryLevel":1,"Name":"PacSnackz","Personal_Status_Message":"","Rank_Stat_Conquest":0,"Rank_Stat_Duel":0,"Rank_Stat_Joust":0,"RankedConquest":{"Leaves":0,"Losses":0,"Name":"League","Points":0,"PrevRank":0,"Rank":0,"Rank_Stat_Conquest":null,"Rank_Stat_Duel":null,"Rank_Stat_Joust":null,"Season":0,"Tier":0,"Trend":0,"Wins":0,"player_id":null,"ret_msg":null},"RankedDuel":{"Leaves":0,"Losses":0,"Name":"Duel","Points":0,"PrevRank":0,"Rank":0,"Rank_Stat_Conquest":null,"Rank_Stat_Duel":null,"Rank_Stat_Joust":null,"Season":0,"Tier":0,"Trend":0,"Wins":0,"player_id":null,"ret_msg":null},"RankedJoust":{"Leaves":0,"Losses":0,"Name":"Joust","Points":0,"PrevRank":0,"Rank":0,"Rank_Stat_Conquest":null,"Rank_Stat_Duel":null,"Rank_Stat_Joust":null,"Season":0,"Tier":0,"Trend":0,"Wins":0,"player_id":null,"ret_msg":null},"Region":"North America","TeamId":0,"Team_Name":"","Tier_Conquest":0,"Tier_Duel":0,"Tier_Joust":0,"Total_Achievements":28,"Total_Worshippers":510,"Wins":35,"ret_msg":null}]
I am using Spring Boot Java and Google's Gson library. I have looked into Jackson as well with no luck either.
I figured out the answer. The trick was to set Player to an array. For other objects I had I used a for loop to return multiple objects. For this example it is set to 0 since there is only one player.
#RequestMapping("/player")
public Player displayPlayer() throws Exception{
String player = "PacSnackz";
smite.createSession();
String test = smite.getPlayer(player);
Gson gson = new Gson();
Player[] playerObj = gson.fromJson(test, Player[].class);
return playerObj[0];
}

How to convert this json String to normal java arraylist using gson

I tried to convert following JSON string into Array and got following error:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError:
org/apache/commons/logging/LogFactory at
net.sf.json.AbstractJSON.(AbstractJSON.java:54) at
net.sf.json.util.CycleDetectionStrategy.(CycleDetect‌​ionStrategy.java:36)
at net.sf.json.JsonConfig.(JsonConfig.java:65) at
net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
JSON:
[
{
"file_name":"1.xml",
"file_ext":"application/octet-stream",
"sr_no":"0.1",
"status":"Checked ",
"rev":"1",
"locking":"0"
},
{
"file_name":"2.xml",
"file_ext":"json/octet-stream",
"sr_no":"0.2",
"status":"Not Checked ",
"rev":"2",
"locking":"1"
},
{
"file_name":"3.xml",
"file_ext":"application/json-stream",
"sr_no":"0.3",
"status":"Checked ",
"rev":"1",
"locking":"3"
},
{
"file_name":"4.xml",
"file_ext":"application/octet-stream",
"sr_no":"0.4",
"status":"Checked ",
"rev":"0.4",
"locking":"4"
}
]
Code:
JSONArray nameArray = (JSONArray) JSONSerializer.toJSON(output);
System.out.println(nameArray.size());
for(Object js : nameArray)
{
JSONObject json = (JSONObject) js;
System.out.println("File_Name :" +json.get("file_name"));
}
I know the question is about converting JSON String to Java Array, but I would like to also answer about how to convert the JSON String to an ArrayList using the Gson Library.
Since I spend a good amount of time in solving this, I hope my solution may help others.
My JSON string looks similar to this one -
I had an object named StockHistory, and I wanted to convert this JSON into an ArrayList of StockHistory.
This is how my StockHistory class looked -
class StockHistory {
Date date;
Double open;
Double high;
Double low;
Double close;
Double adjClose;
Double volume;
}
The code that I used to convert the JSON Array to the ArrayList of StockHistory is as follows -
Gson gson = new Gson();
Type listType = new TypeToken< ArrayList<StockHistory> >(){}.getType();
List<StockHistory> history = gson.fromJson(reader, listType);
Now if you are reading your JSON from a file, the reader's initialization would be -
Reader reader = new FileReader(fileName);
and if you are just converting a string to JSON object then, the reader's initialization would simply be -
String reader = "{ // json String }";
Hope that helps. Cheers!!!
You can create a java class with entities are: file_name, file_ext, sr_no, status, rev, locking in string type.
public class TestJson {
private String file_name, file_ext, sr_no, status, rev, locking;
//get & set
}
}
Then you call:
public static void main(String[] args) {
String json = your json string;
TestJson[] respone = new Gson().fromJson(json, TestJson[].class);
for (TestJson s : respone) {
System.out.println("File name: " + s.getFile_name());
}
}
So, you have a list of object you want.
Firstly I have to say your question is quite "ugly" and next time please improve your question's quality.
Answer:
Try to use com.fasterxml.jackson.databind.ObjectMapper
If you have a java class to describe your items in the list:
final ObjectMapper mapper = new ObjectMapper();
YourClass[] yourClasses = mapper.readValue(YourString, YourClass[].class);
Then convert the array to a List.
If you don't have a java class, just you LinkedHashMap instead.

GSON parsing unspecified type variable

I parse server JSON response with GSON library. Backend guys sometimes tell me: "We can't specify variable type in JSON for some reason" (old php, they don't know how to do it and so on and so forth).
GSON likes strong typing in its object model. So I can't parse Object as String.
GSON wait for:
{
"service":{
"description":null,
"name":"Base",
"id":"4c7a90410529"
}
}
But it gets (empty data):
"service": ""
And I get
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1396
What is the best practice to parse such response?
Another question:
How can I build object, it can recognize Integer variable which returned from time to time as Integer or as String? The same server side issue.
"data": "1"
or
"data": 1
I know - we should use specific types in Java. But sometime it is worth to make concessions,
Thanks
EDIT:
My solution based on Java Developer's answer.
ServiceDeserializer class deserialize every object depending on its internal value.
public class ServiceDeserializer implements JsonDeserializer<ServiceState>{
#Override
public ServiceState deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String name = "";
String uuid = "";
String description = "";
if (json.isJsonObject()) {
JsonObject obj = json.getAsJsonObject();
if (!obj.get("name").isJsonNull()) {
name = obj.get("name").getAsString();
}
if (!obj.get("uuid").isJsonNull()) {
uuid = obj.get("uuid").getAsString();
}
if (!obj.get("description").isJsonNull()) {
description = obj.get("description").getAsString();
}
}
return new ServiceState(name, uuid, description);
}
}
And my GSON constructor with type adapter for ServiceState.
Gson gson = new GsonBuilder()
.registerTypeAdapter(ServiceState.class, new ServiceDeserializer())
.create();
You need to scrape the JSON response before trying to deserialize it into your Response Java object. You can make use of Java org.json parser to verify that service object actually exists and fix it otherwise.
String json = "{\"service\":{\r\n" +
" \"description\":null,\r\n" +
" \"name\":\"Base\",\r\n" +
" \"id\":\"4c7a90410529\"\r\n" +
"}}";
String json2 = "{\"service\":\"\"}";
JSONObject root = new JSONObject(json);
// JSONObject root = new JSONObject(json2);
if (root.optJSONObject("service") == null) {
root.put("service", new JSONObject());
}
Gson gson = new Gson();
Response response = gson.fromJson(root.toString(), Response.class);
System.out.println(response.getService());
Output :
// for JSONObject root = new JSONObject(json);
Service [id=4c7a90410529, name=Base, description=null]
// for JSONObject root = new JSONObject(json2);
Service [id=null, name=null, description=null]
Secondly, Gson is smart enough to do simple conversions like String to Integer etc. So, deserializing such JSON properties shouldn't give you any troubles.
System.out.println(gson.fromJson("10", Integer.class)); // 10
System.out.println(gson.fromJson("\"20\"", Integer.class)); // 20
If you want to stick with strictly gson you can provide a custom deserializer. Since we know that service is either a property of the base json string or embedded within some other property, we can use the deserializer to step-wise parse out the offending components and handle them accordingly.
public class MyJsonDeserializer implements JsonDeserializer<YourParsedData> {
#Override
public YourParsedData deserialize(final JsonElement je, final Type type, final JsonDeserialization Context jdc) throws JsonParseException
{
final JsonObject obj = je.getAsJsonObject(); //our original full json string
final JsonElement serviceElement = obj.get("service");
//here we provide the functionality to handle the naughty element. It seems emtpy string is returned as a JsonPrimitive... so one option
if(serviceElement instanceOf JsonPrimitive)
{
//it was empty do something
}
return YourParsedData.create(); //provide the functionality to take in the parsed data
}
}
The custom deserializer would be called as follows:
final Gson gson = new GsonBuilder().registerTypeAdapter(YourParsedData.class, new MyJsonDeserializer()).create();
gson.fromJson("{service: ''}", YourParsedData.class);
I typed all this up so if I missed some syntax my apologies.
Your json is invalid and any Json parser wouldn't be able to parse a syntactically incorrect json:
"service": {
"description": null,
"name": "Base",
"id": "4c7a90410529"
}
should be encapsulated in curly braces as mentioned here:
{
"service": {
"description": null,
"name": "Base",
"id": "4c7a90410529"
}
}
A json structure is enclosed within {}. Your response seems to be missing that. You can manually append { and } at the beginning and end of the string to make it into a valid json structure.
Once this is done, you can use Gson to parse your json response normally.
What is the best practice to parse such response?
Use a good enough Json parser. That's more than enough. And try to have a class representing the exact same Structure as the response to avoid parsing the json responses level by level, manually.

Categories