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
Related
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];
}
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.
We have a requirement to update the JSON data in middle and need to return the updated JSON data using java. Also it should support any type of JSON data.
ex:
Assume {object:{"color":"red","shape":"Triangle"}} is the JSON data and in this we need to update the shape value to Rectangle and we need to return the updated JSON data as below:
{object:{"color":"red","shape":"Rectangle"}}
For this we need to pass the element path ( which element we need to update) and updateText and JSON Data to the JAVA code.
here is the methodCall:
updateValue("object/shape", "Rectangle", "{object:{"color":"red","shape":"Triangle"}}")
We tried below code using Gson library. But with this code we are able to update the targeted Json element, but the requirement is to return the entire JSON data with the updated value.
So please suggest how do we re-build the JSON data with the updated text.
Below is the code we tried to update the Json Data.
public String updateValue(String keyPath, String updateText, String jsonText) {
String[] keys = keyPath.split("/");
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonText);
String result = "";
for(String key : keys)
{
if (jsonObject.get(key) instanceof JsonObject)
{
jsonObject = (JsonObject)jsonObject.get(key);
}
else if(jsonObject.get(key) instanceof JsonArray)
{
JsonArray jsonArray = (JsonArray)jsonObject.get(key);
result = jsonArray.toString();
}
else
{
result = jsonObject.get(key).toString();
}
}
result = result.replace(result, updateText);
return result;
}
The problem lies in the way you do the replacements. When you translate the JsonObject to String, you lose the object, and after replacement, you just have the replaced String. To fix it, you need to operate directly on the object, instead of the String counterpart. Because JsonObject is mutable, holding a reference to the input will reflect the changes. One drawback is you can't replace a value in a JsonArray this way, partly because you don't know which element to replace. To accomplish that, you will need a little more in the input(either the value to replace or the element position).
public String updateValue(String keyPath, String updateText, String jsonText) {
String[] keys = keyPath.split("/");
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonText);
JsonObject returnVal = jsonObject; // This holds the ref to target json object
JsonPrimitive jp = new JsonPrimitive(updateText);
String finalKey = keys[keys.length - 1];
for(String key : keys)
{
if (jsonObject.get(key).isJsonObject())
{
jsonObject = (JsonObject)jsonObject.get(key);
}
}
jsonObject.remove(finalKey);
jsonObject.add(finalKey, jp);
return returnVal.toString();
}
You can use JsonPath lib for that and try using the following code.
private static final Configuration configuration = Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.mappingProvider(new JacksonMappingProvider())
.build();
JsonNode updatedJson = JsonPath.using(configuration).parse(originaljson)
.set("use the path to go for value", "new value").json();
json = updatedJson.toString();
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.
I am getting the data from the Zookeeper node like this
byte[] bytes = client.getData().forPath("/my/example/node1");
String ss = new String(bytes);
Here ss will have data like this which is a simple JSON String consisting of key value pair -
{"description":"Some Text", "machinename":"machineA", "ipaddress":"192.128.0.0"}
Now I want to append one more key value pair at the end to the above JSON String. This is the below key value pair I want to append -
"version":"v3"
So the final JSON String will look like this -
{"description":"Some Text", "machinename":"machineA", "ipaddress":"192.128.0.0", "version":"v3"}
What's the best and efficient way to do this?
Use a JSON Parser/Generator to parse your given JSON to a tree structure and then add your JSON field.
With Gson, that would look something like this
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(ss, JsonObject.class); // parse
jsonObject.addProperty("version", "v3"); // modify
System.out.println(jsonObject); // generate
prints
{"description":"Some Text","machinename":"machineA","ipaddress":"192.128.0.0","version":"v3"}
Will Zookeeper always return valid JSON or their custom format? Be aware of that.
When it comes to JSON processing, string manipulation only works in special and simple cases. For the general case, a good JSON parser library should be used.
Jackson is among the top of such libraries in terms of performance, efficiency, versatility and reliability, plus it is published under the commercial-friendly Apache 2.0 license.
Following is a simple implementation of the requested answer in Jackson.
public static void main(String[] args)
{
String ss = "{\"description\":\"Some Text\", \"machinename\":\"machineA\", \"ipaddress\":\"192.128.0.0\"}";
System.out.println("JSON string before: " + ss);
try
{
ObjectMapper mapper = new ObjectMapper();
Map<String, String> map = (Map<String, String>)mapper.readValue(ss, Map.class);
map.put("version", "v3");
ss = mapper.writeValueAsString(map);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("JSON string after: " + ss);
}
Basic string manipulation. Insert your additional string before the final close brace }. Make sure to add a comma.
Json objects don't need to be ordered.
String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
String json2 = "\"version\":\"v3\"";
json2 = ',' + json2;
String json3 = json.substring(0,json.length()-1) + json2 + json.charAt(json.length()-1);
That should be the simplest, most efficient way, if that's all you need to do.
For additional reading on String manipulation,
http://docs.oracle.com/javase/tutorial/java/data/manipstrings.html