I am writing a Java parser that get ASANA APP data from ASANA server and parse it and then save it to local DB for customized reports. I have issue with the time needed to execute this Java utility.
I have this sub part of the Java parser. When I run this, it takes approximately 10 seconds to execute. If this code executes in 10 sec than one line this is given below:
JSONObject Project_jsonObject = (JSONObject) Project_jsonParser.parse(Project_br);
This line execute in 9 seconds out of total 10 seconds. How I can reduce this time. Any technique or tip or alternate to reduce time.Please help me I am waiting
static File workDir = new File("C:/cygwin64/bin");
static Runtime systemShell = Runtime.getRuntime();
String project_request="curl -u 4cKDgv4O.L8Th7N8l7jADg3HRU44abmT: https://app.asana.com/api/1.0/projects/"+id_Project+"?opt_pretty";
String project_cmd = project_request;
project_cmd += " | grep 'OBJECT'";
Process ProjectshellOutput = systemShell.exec(workDir+"/"+project_cmd, null,workDir);
InputStreamReader Project_isr = new InputStreamReader(ProjectshellOutput.getInputStream());
BufferedReader Project_br = new BufferedReader (Project_isr);
JSONParser Project_jsonParser = new JSONParser();
JSONObject Project_jsonObject = (JSONObject) Project_jsonParser.parse(Project_br);
JSONObject projectdataObject= (JSONObject)Project_jsonObject.get("data");
Long project_id = (Long) projectdataObject.get("id");
System.out.println("project id is: " + project_id);
String project_created_at_dt = (String) projectdataObject.get("created_at");
System.out.println("The project created at is: " + project_created_at_dt);
String project_modified_at_dt = (String) projectdataObject.get("modified_at");
System.out.println("The project modified at is: " + project_modified_at_dt);
boolean project_public_status = (boolean) projectdataObject.get("public");
System.out.println("project public status: " + project_public_status);
String project_name = (String) projectdataObject.get("name");
System.out.println("project name is: " + project_name);
String project_notes = (String) projectdataObject.get("notes");
System.out.println("project Notes are: " + project_notes);
boolean project_archived_status = (boolean) projectdataObject.get("archived");
System.out.println("project archived status: " + project_archived_status);
JSONObject workspaceObj = (JSONObject) projectdataObject.get("workspace");
Long workspace_id = (Long) workspaceObj.get("id");
String workspace_name = (String) workspaceObj.get("name");
System.out.println("Workspace id " + workspace_id + " with workspace name " + workspace_name);
String color = (String) projectdataObject.get("color");
System.out.println("color : " + color);
JSONObject teamObj = (JSONObject) projectdataObject.get("team");
Long team_id = (Long) teamObj.get("id");
String team_name = (String) teamObj.get("name");
System.out.println("team id " + team_id + " with team name " + team_name);
It looks like you're using the JSON.simple library. According to this test, JSON.simple parsed a 190 MB file in about 140 seconds. That's in line with taking 9 seconds to parse a 15 MB file. Jackson is faster, but only just barely. So I doubt there's anything you can do to significantly improve performance.
Related
This could be a duplicate question, but I couldn't find my solution anywhere. Hence, posting it.
I am trying to simply POST a request for a Student account Creation Scenario. I do have a JSON file which comprises all the "Keys:Values", required for Student account creation.
This is how the file student_Profile.json looks like:
{
"FirstName":"APi1-Stud-FN",
"MiddleInitial":"Q",
"LastName":"APi1-Stud-LN",
"UserAlternateEmail":"",
"SecretQuestionId":12,
"SecretQuestionAnswer":"Scot",
"UserName":"APi1-stud#xyz.com",
"VerifyUserName":"APi1-stud#xyz.com",
"Password":"A123456",
"VerifyPassword":"A123456",
"YKey":"123xyz",
"YId":6,
"Status":false,
"KeyCode":"",
"SsoUserName":"APi1-stud#xyz.com",
"SsoPassword":"",
"BirthYear":2001
}
So everything on Posting the request from "Rest Assured" point of view looks fine, it's just that I want to update a few values from the above JSON body using JAVA so that I can create a new Student profile every time I run my function and don't have to manually change the Body.
For Every POST Student Account Creation scenario, I need to update the value for
the following keys so that a new test student user account can be created:
First Name
Last Name and
Username // "VerifyUserName" and "SSO UserName" will remain same as user name
I modified the answer to get random values and pass them to json body. random value generation was taken from the accepted answer of this question.
public void testMethod() {
List<String> randomValueList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < 18) { // length of the random string.
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
randomValueList.add(salt.toString());
}
String jsonBody = "{\n" +
" \"FirstName\":\"" + randomValueList.remove(0) + "\",\n" +
" \"MiddleInitial\":\"Q\",\n" +
" \"LastName\":\"" + randomValueList.remove(0) + "\",\n" +
" \"UserAlternateEmail\":\"\",\n" +
" \"SecretQuestionId\":12,\n" +
" \"SecretQuestionAnswer\":\"Scot\",\n" +
" \"UserName\":\"" + randomValueList.remove(0) + " \",\n" +
" \"VerifyUserName\":\"APi1-stud#xyz.com\",\n" +
" \"Password\":\"A123456\",\n" +
" \"VerifyPassword\":\"A123456\",\n" +
" \"YKey\":\"123xyz\",\n" +
" \"YId\":6,\n" +
" \"Status\":false,\n" +
" \"KeyCode\":\"\",\n" +
" \"SsoUserName\":\"APi1-stud#xyz.com\",\n" +
" \"SsoPassword\":\"\",\n" +
" \"BirthYear\":2001\n" +
"}";
Response response = RestAssured
.given()
.body(jsonBody)
.when()
.post("api_url")
.then()
.extract()
.response();
// Do what you need to do with the response body
}
We can used pojo based approach to do certain things very easily . No matter how complex is the payload , serialization and dieselization is the best answer . I have created a framework template for api automation that can we used by putting required POJO's in path :
https://github.com/tanuj-vishnoi/pojo_api_automation
To create pojo, I also have ready to eat food for you :
https://github.com/tanuj-vishnoi/pojo_generator_using_jsonschema2pojo
for the above problem you can refer to the JsonPath lib https://github.com/json-path/JsonPath and use this code:
String mypayload = "{\n" +
" \"FirstName\":\"APi1-Stud-FN\",\n" +
" \"MiddleInitial\":\"Q\",\n" +
" \"LastName\":\"APi1-Stud-LN\"}";
Map map = JsonPath.parse(mypayload).read("$",Map.class);
System.out.println(list);
once the payload converted into map you can change only required values as per the requirement
To generate random strings you can refer to lib org.apache.commons.lang3.RandomStringUtils;
public static String generateUniqueString(int lenghtOfString){
return
RandomStringUtils.randomAlphabetic(lenghtOfString).toLowerCase();
}
I recommend to store payload in a separate file and load it at runtime.
I'll like to get only the BTC pairs of Binance. I use this code to get all currencies current prices
List<TickerPrice> allPrices = client.getAllPrices();
System.out.println(allPrices);
How do I turn only BTC pairs? Thank, experts in the house.
try and catch BinanceAPIException around .. example of grabbing a pair
BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance();
BinanceApiRestClient client = factory.newRestClient();
// BinanceApiAsyncRestClient client = factory.newAsyncRestClient();
// Test connectivity
client.ping();
long serverTime = client.getServerTime();
out.println("<p> Time of ticker from Binance - its Servertime : " + serverTime + "</p>");
TickerStatistics tickerStatistics = client.get24HrPriceStatistics("NEOETH");
out.println("<p> NEO ETH : " + tickerStatistics.getLastPrice() + "</p>");
String price = tickerStatistics.getLastPrice().toString();
out.println("<p> as string .. " + price + "</p>");
TickerStatistics tickerStatistics2 = client.get24HrPriceStatistics("HBARUSDT");
out.println("<p> HBAR USDT : " + tickerStatistics2.getLastPrice() + "</p>");
I got the following json:
{
"ID": "1234567",
"dangereousCargo": true,
"numberOfPassangers": 164,
"cargo": [
{
"type": "Oil",
"amount": 8556
},
{
"type": "Chemicals",
"amount": 5593
}
]
}
From this question, I understood that it is possible to get the cargoList out of the jsonObject (if that list contains a certain type of object). But how do I get the seperate cargoObjects out of that list?
+Do the variable names of the jsonstring have to correspond with the variable names in my CargoClass? What if the jsonObject only contains type and amount and my CargoClass has more attributes?
You can iterate throws the JSONArray which represents your cargo list doing (not tested)
JSONObject json = new JSONObject(jsonString);
JSONArray cargoList = json.getJSONArray("cargo");
for(int i=0; i< cargoList.length(); i++) {
JSONObject cargo = cargoList.getJSONObject(i);
//Do something with cargo
}
But how do I get the seperate cargoObjects out of that list?
String jsonString = "{ ... }";
JSONObject json = new JSONObject(jsonString);
JSONArray cargoList = json.getJSONArray("cargo");
for(JSONObject cargo : cargoList)
{
//do something with your cargo element
}
Do the variable names of the jsonstring have to correspond with the
variable names in my CargoClass?
If you use the the get method from the JSONObject, you have to specify the exact name of the attribute in your jsonString. Following the example above:
String cargoType = cargo.getString("type");
By the way, if you want to use your already defined CargoClass, you need a Deserializer and all the attributes on your JSON must be the all present and all the same on your CargoClass: I suggest you to take a look at other SOs questions like this one.
What if the jsonObject only contains type and amount and my CargoClass
has more attributes?
The other attributes will be initialize in base of your class declaration
Using JSON Simple it is very easy to parse and read your data from your JSON. As other users have said there are a plethora of libraries out there that can accomplish this. Below is a tested example of your code.
public static void main(String[] args) throws JSONException {
String json = "{\n"
+ " ID: 1234567,\n"
+ " dangereousCargo: true,\n"
+ " numberOfPassangers: 164,\n"
+ " cargo: [\n"
+ " {\n"
+ " type: Oil,\n"
+ " amount: 8556\n"
+ " },\n"
+ " {\n"
+ " type: Chemicals,\n"
+ " amount: 5593\n"
+ " }\n"
+ " ]\n"
+ "}";
JSONObject data = new JSONObject(json);
int id = data.getInt("ID");
boolean danger = data.getBoolean("dangereousCargo");
int numOfPassengers = data.getInt("numberOfPassangers");
System.out.println("Current ID: " + id + "\n"
+ "Is Dangerous: " + danger + "\n"
+ "Number of Passengers: " + numOfPassengers + "\n");
JSONArray cargo = data.getJSONArray("cargo");
NumberFormat currency = NumberFormat.getCurrencyInstance();
for (int i = 0; i < cargo.length(); i++) {
JSONObject cargoObject = cargo.getJSONObject(i);
String type = cargoObject.getString("type");
double amount = cargoObject.getDouble("amount");
System.out.println("Current Type: " + type);
System.out.println("Current Amount: " + currency.format(amount));
}
}
}
The above code gives us the following output:
Once you have your data you can really do whatever you want with it.
Libraries
JSONSimple-https://code.google.com/p/json-simple/
GSON-https://github.com/google/gson
I am using the Java library Metadata-extractor and cannot extract the tag
description correctly using the getUserCommentDescription method code below,
although the tag.getDescription does work:
String exif = "File: " + file;
File jpgFile = new File(file);
Metadata metadata = ImageMetadataReader.readMetadata(jpgFile);
for (Directory directory : metadata.getDirectories()) {
String directoryName = directory.getName();
for (Tag tag : directory.getTags()) {
String tagName = tag.getTagName();
String description = tag.getDescription();
if (tagName.toLowerCase().contains("comment")) {
Log.d("DEBUG", description);
}
exif += "\n " + tagName + ": " + description; //Returns the correct values.
Log.d("DEBUG", directoryName + " " + tagName + " " + description);
}
if (directoryName.equals("Exif IFD0")) {
// create a descriptor
ExifSubIFDDirectory exifDirectory = metadata.getDirectory(ExifSubIFDDirectory.class);
ExifSubIFDDescriptor descriptor = new ExifSubIFDDescriptor(exifDirectory);
Log.d("DEBUG","Comments: " + descriptor.getUserCommentDescription()); //Always null.
}
Am I missing something here?
You are checking for the directory name Exif IFD0 and then accessing the ExifSubIFDDirectory.
Try this code outside the loop:
Metadata metadata = ImageMetadataReader.readMetadata(jpgFile);
ExifSubIFDDirectory exifDirectory = metadata.getDirectory(ExifSubIFDDirectory.class);
ExifSubIFDDescriptor descriptor = new ExifSubIFDDescriptor(exifDirectory);
String comment = descriptor.getUserCommentDescription();
If this returns null then it may be an encoding issue or bug. If you run this code:
byte[] commentBytes =
exifDirectory.getByteArray(ExifSubIFDDirectory.TAG_USER_COMMENT);
Do you have bytes in the array?
If so then please open an issue in the issue tracker and include a sample image that can be used to reproduce the problem. You must authorise any image you provide for use in the public domain.
I am stuck up in this date range query. I need to extract data from particular facebook pages for a specified date range.I am able to do this individually, by using since and until fields. But how to use these two fields together.
Here is my code:
public static String getFacebookPostes(Facebook facebook, String searchPost)
throws FacebookException {
String searchResult = "Item : " + searchPost + "\n";
StringBuffer searchMessage = new StringBuffer();
ResponseList<Post> results = facebook.searchPosts(searchPost, new Reading().since("2014-04-02"));
String userId="";
for (Post post : results) {
System.out.println(post.getMessage());
searchMessage.append(post.getMessage() + "\n");
for (int j = 0; j < post.getComments().size(); j++) {
searchMessage.append(post.getComments().get(j).getFrom()
.getName()
+ ", ");
searchMessage.append(post.getComments().get(j).getMessage()
+ ", ");
searchMessage.append(post.getComments().get(j).getCreatedTime()
+ ", ");
searchMessage.append(post.getComments().get(j).getLikeCount()
+ "\n");
userId=post.getComments().get(j).getFrom().getId();
User user = facebook.getUser(userId);
//System.out.println("ROCK");
System.out.println(user);
}
}
Any guidance is appreciated. Thanks in advance.
PS : I am using facebook4j-core-2.0.2.jar and eclipse kepler.
According to http://facebook4j.org/en/code-examples.html you can use all date formats descirbed in http://www.php.net/manual/en/datetime.formats.date.php
From my understanding the code would then look like this:
ResponseList<Post> results = facebook.searchPosts(searchPost, new Reading().since("2014/04/02").until("2014/04/08"));