I'm new to development part using API and JSON file, so be patient with me. I'm trying to implement a Rest API in java where I need to send a Post request to an URL to process but when I tried it in my localhost, the result is that what I receive is just empty Json file.
I was using the dependency from org.json.simple.JSONObject, but I need to change the dependency to org.json.JSONObject. I know that they are two different library and that's why I am a bit stuck. I looked on the forum and on the internet but I didn't find a solution for my own problem. If it is possible I want to also ask if there is a way to convert a String to a JSON.
Here is the main class.
public class DataService {
public static JSONObject processData(JSONObject jsonObject) {
System.out.println(jsonObject);
System.out.println(jsonObject.toString());
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Data data = new Data();
try {
data = mapper.readValue(jsonObject.toString(), Data.class);
} catch (IOException e) {
e.printStackTrace();
}
List<DataJson> timeserie = data.getData();
List<Double> values = new ArrayList<Double>();
DataJson inter;
for (int i = 0; i<timeserie.size(); i++){
inter = timeserie.get(i);
values.add(inter.getValue());
}
int EmbeddingDimension;
EmbeddingDimension = data.getEmbeddingDimension();
data.setResult(DynamicProperties.PermEn(values, EmbeddingDimension));
String url = "http://localhost:8080/crosscpp/toolbox/test";
OkHttpClient client = new OkHttpClient();
ObjectMapper objectMapper = new ObjectMapper();
RequestBody body = null;
try {
body = RequestBody.create(
MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(data));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = client.newCall(request);
try {
Response response = call.execute();
String result = response.body().string();
JSONObject json = null;
/*JSONParser parser = new JSONParser();
JSONObject json = null;
try {
json = (JSONObject) parser.parse(result);
} catch (ParseException e) {
e.printStackTrace();
}*/ //Look for another solution.
return json;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
The Json file I will send.
{
"inputAeonSubscriptionUrl": "xxxx",
"outputAeonPublicationUrl": "xxxx",
"EmbeddingDimension": 3,
"offerId": "xxxxxx",
"measurement-channel-id": "1",
"id": "xxxxxx",
"submissionDate": {
"min": "2019-04-09",
"max": "2019-05-07"
},
"travelDates": {
"min": "2019-05-13",
"max": "2019-05-17"
},
"travelledDuration": {
"min": 1,
"max": 2
},
"geoBoundingBox": {
"latitude-max": 51.507561,
"latitude-min": 51.497715,
"longitude-min": 7.482349,
"longitude-max": 7.500885
},
"data": [
{
"value": 1,
"timestamp": "2019-04-09"
},
{
"value": 3,
"timestamp": "2019-04-09"
},
{
"value": 2,
"timestamp": "2019-04-09"
},
{
"value": 1,
"timestamp": "2019-04-09"
},
{
"value": 2,
"timestamp": "2019-04-10"
},
{
"value": 3,
"timestamp": "2019-04-10"
},
{
"value": 2,
"timestamp": "2019-04-10"
}
]
}
I am expecting a result where it send back the received JSON file with an added attribute where it shows the process done on the values.
Related
I'm generating a JSON file in JAVA. The file contains a list of JSONs. I want to import this file to Azure Cosmos DB as soon as it is created.
Is there some way to achieve it from Java code?
Thanks in advance!
According to my research, if we want to implement bulk operations with java, we just can use bulk executor Java library. For more details, please refer to the document and article. Regarding how to use bulk executor Java library, please refer to the document.
For example
My .json file
[{
"id": "1",
"name": "test1",
"age": "20"
}, {
"id": "2",
"name": "test2",
"age": "21"
}, {
"id": "3",
"name": "test3",
"age": "22"
}, {
"id": "4",
"name": "test4",
"age": "23"
},
{
"id": "5",
"name": "test5",
"age": "24"
}, {
"id": "6",
"name": "test6",
"age": "25"
}, {
"id": "7",
"name": "test7",
"age": "26"
}, {
"id": "8",
"name": "test8",
"age": "27"
}
]
My pom.xml
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>documentdb-bulkexecutor</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
Code
String endpoint="<your cosmos db endpoint>";
String key="<your key>";
ConnectionPolicy connectionPolicy = new ConnectionPolicy();
connectionPolicy.setMaxPoolSize(1000);
DocumentClient client = new DocumentClient(
endpoint,
key,
connectionPolicy,
ConsistencyLevel.Session);
String databaseId="testbulk";
String collectionId="items";
String databaseLink = String.format("/dbs/%s", databaseId);
String collectionLink = String.format("/dbs/%s/colls/%s", "testbulk", collectionId);
ResourceResponse<Database> databaseResponse = null;
Database readDatabase = null;
try {
databaseResponse = client.readDatabase(databaseLink, null);
readDatabase = databaseResponse.getResource();
System.out.println("Database already exists...");
} catch (DocumentClientException dce) {
if (dce.getStatusCode() == 404) {
System.out.println("Attempting to create database since non-existent...");
Database databaseDefinition = new Database();
databaseDefinition.setId(databaseId);
client.createDatabase(databaseDefinition, null);
databaseResponse = client.readDatabase(databaseLink, null);
readDatabase = databaseResponse.getResource();
} else {
throw dce;
}
}
ResourceResponse<DocumentCollection> collectionResponse = null;
DocumentCollection readCollection = null;
try {
collectionResponse = client.readCollection(collectionLink, null);
readCollection = collectionResponse.getResource();
System.out.println("Collection already exists...");
} catch (DocumentClientException dce) {
if (dce.getStatusCode() == 404) {
System.out.println("Attempting to create collection since non-existent...");
DocumentCollection collectionDefinition = new DocumentCollection();
collectionDefinition.setId(collectionId);
PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition();
Collection<String> paths = new ArrayList<String>();
paths.add("/id");
partitionKeyDefinition.setPaths(paths);
collectionDefinition.setPartitionKey(partitionKeyDefinition);
RequestOptions options = new RequestOptions();
options.setOfferThroughput(1000000);
// create a collection
client.createCollection(databaseLink, collectionDefinition, options);
collectionResponse = client.readCollection(collectionLink, null);
readCollection = collectionResponse.getResource();
} else {
throw dce;
}
}
System.out.println(readCollection.getId());
System.out.println(readDatabase.getId());
ArrayList<String> list = new ArrayList<String>();
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("e:\\test.json")) {
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray jsonArray = (JSONArray) obj;
System.out.println(jsonArray);
// cast jsonarry to string list
if (jsonArray != null) {
int len = jsonArray.size();
for (int i=0;i<len;i++){
list.add(jsonArray.get(i).toString());
}
}
System.out.println(list.get(0));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
// Set client's retry options high for initialization
client.getConnectionPolicy().getRetryOptions().setMaxRetryWaitTimeInSeconds(30);
client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(9);
// Builder pattern
DocumentBulkExecutor.Builder bulkExecutorBuilder = DocumentBulkExecutor.builder().from(
client,
databaseId,
collectionId,
readCollection.getPartitionKey(),
20000) ;// throughput you want to allocate for bulk import out of the container's total throughput
// Instantiate DocumentBulkExecutor
try {
DocumentBulkExecutor bulkExecutor = bulkExecutorBuilder.build();
// Set retries to 0 to pass complete control to bulk executor
client.getConnectionPolicy().getRetryOptions().setMaxRetryWaitTimeInSeconds(0);
client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(0);
BulkImportResponse bulkImportResponse = bulkExecutor.importAll(list, false, false, null);
System.out.println(bulkImportResponse.getNumberOfDocumentsImported());
} catch (Exception e) {
e.printStackTrace();
}
I am trying to retreive customer object from my rest api. I generated the api using spring data jpa. I have used volley to retrive the information from the api. I can't tell what i did wrong. As i am new to android i don't have much idea. can some one help me to parse the customer object from my Json api.
my api looks like this:
{
"_embedded": {
"customers": [
{
"firstName": "Alexander",
"lastName": "arnold",
"email": "trentarnold#liverpool.com",
"password": "cornertakenquickly",
"_links": {
"self": {
"href": "http://localhost:8080/api/customers/1"
},
"customer": {
"href": "http://localhost:8080/api/customers/1"
}
}
},
{
"firstName": "test",
"lastName": "tester",
"email": "dulalsujan911#gmail.com",
"password": "12345678",
"_links": {
"self": {
"href": "http://localhost:8080/api/customers/2"
},
"customer": {
"href": "http://localhost:8080/api/customers/2"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/customers{?page,size,sort}",
"templated": true
},
"profile": {
"href": "http://localhost:8080/api/profile/customers"
}
},
"page": {
"size": 20,
"totalElements": 2,
"totalPages": 1,
"number": 0
}
}
my code looks like this i am using volley :
// connects to the api and stores the retrived JSON values in the respective list
public void connectToApi(){
// initialize lists
emailList = new ArrayList<>();
passwordList = new ArrayList<>();
final String BASE_URL ="http://192.168.1.67:8080/api/customers";
// final String BASE_URL ="http://10.0.2.2:8080/api/customers";
// creating a request ques for HTTP request
RequestQueue requestQueue = Volley.newRequestQueue(this);
// Setting HTTP GET request to retrieve the data from the SERVER
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET,BASE_URL
,null
, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("customers");
for(int i=0; i<jsonArray.length();i++){
JSONObject customer = jsonArray.getJSONObject(i);
emailList.add(customer.getString("email"));
passwordList.add(customer.getString("password"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
} , new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("REST error",error.toString() );
}
}
);
requestQueue.add(objectRequest);
}
Do this :
#Override
public void onResponse(JSONObject response) {
try {
JSONObject json = new JSONObject(response);
JSONObject json_embedded = json.getJSONObject("_embedded");// need to access JSONObject("_embedded")
JSONArray jsonArray = json_embedded.getJSONArray("customers"); // then get JSONARRAY
for(int i=0; i<jsonArray.length();i++){
JSONObject customer = jsonArray.getJSONObject(i);
emailList.add(customer.getString("email"));
passwordList.add(customer.getString("password"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
NOTE : your json array (customers) is in _embedded that's why
it is showing exception.
You need to access first to _embedded object.
try {
JSONObject embedded = response.getJSONObject("_embedded");
JSONArray jsonArray = embedded.getJSONArray("customers");
for(int i=0; i<jsonArray.length();i++){
JSONObject customer = jsonArray.getJSONObject(i);
emailList.add(customer.getString("email"));
passwordList.add(customer.getString("password"));
}
} catch (Exception e) {
e.printStackTrace();
}
I am trying to update the remote JSON values using Volley for Android. Problem is that the code below completely overrides the whole JSON object.
File is located here: https://api.myjson.com/bins/kubxi
Original JSON file looks like this:
{
"females": [
{
"id": 1,
"name": "Name One",
"actions": [
{
"action_1": 1,
"action_2": 2,
"action_3": 3
}
]
},
{
"id": 2,
"name": "Name Two",
"actions": [
{
"action_1": 4,
"action_2": 5,
"action_3": 6
}
]
}
]
}
Java code
private void sendRequest() {
RequestQueue queue = Volley.newRequestQueue(this);
final JSONObject jsonObject = new JSONObject();
String url ="https://api.myjson.com/bins/kubxi"; // Remote JSON file
try {
jsonObject.put("action_1", 123);
jsonObject.put("action_2", 456);
jsonObject.put("action_3", 789);
} catch (JSONException e) {
Log.d("Exception", e.toString());
}
JsonObjectRequest putRequest = new JsonObjectRequest(Request.Method.PUT, url, jsonObject,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
}
)
{
#Override
public Map<String, String> getHeaders()
{
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
return headers;
}
#Override
public byte[] getBody() {
try {
Log.i("JSON", jsonObject.toString());
return jsonObject.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
};
queue.add(putRequest);
}
After using this code, JSON file looks like this:
{
"action_1": 123,
"action_2": 456,
"action_3": 789
}
I was expecting for the code to only update the values on action_1, action_2 and action_3 from 1, 2, 3 to 123, 456, 789.
I want the JSON file to look like that after running the code:
{
"females": [
{
"id": 1,
"name": "Name One",
"actions": [
{
"action_1": 123,
"action_2": 456,
"action_3": 789
}
]
},
{
"id": 2,
"name": "Name Two",
"actions": [
{
"action_1": 123,
"action_2": 456,
"action_3": 789
}
]
}
]
}
Suggestions will be appreciated!
To update particular value in json file ,you can do like this:
Firstly take your original json in String :
String jsonString ="{
"females": [
{
"id": 1,
"name": "Name One",
"actions": [
{
"action_1": 1,
"action_2": 2,
"action_3": 3
}
]
}
]
}";
Next ,pass this String in JsonObject:
JSONObject jObject = new JSONObject(jsonString);//passing string to jsonobject
JSONArray jsonArray = jObject.getJSONArray("females");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
JSONArray jsonObject= object.getJSONArray("actions"); //getting action
array
for (int j = 0; j < jsonObject.length(); j++) {
JSONObject object1 = jsonObject.getJSONObject(j);
object1.put("action_1", 123); //here you are putting value to action_1
object1.put("action_2", 456);
object1.put("action_3", 789);
}
}
and then send this jsonObject to your server.
I'm having trouble appending to a JSON file. I can add the new entry but not insert it into the correct position.
Code:
public static void main(String args[]) throws Exception {
{
File file = new File("jsonFormatting.json");
if (!file.exists()) {
System.out.println("No file");
} else {
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("jsonFormatting.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray jsonItemInfo = (JSONArray) jsonObject.get("itemInfo");
JSONObject newObject = new JSONObject();
newObject.put("item", new Integer(10003));
newObject.put("name", "ID10003");
StringWriter out = new StringWriter();
newObject.writeJSONString(out);
String jsonText = out.toString();
System.out.println(jsonText);
jsonItemInfo.add(newObject);
FileWriter fileToWrite = new FileWriter("jsonFormatting.json", true);
try {
fileToWrite.write(jsonItemInfo.toJSONString());
} catch (IOException e) {
e.printStackTrace();
}
fileToWrite.flush();
fileToWrite.close();
} catch (IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
JSON file:
"sampleArray": [
"Element_0",
"Element_1"
],
"dataPoint_1": 40,
"dataPoint_2": 500,
"dataPoint_3": 650,
"itemInfo": [
{
"item": "10001",
"name": "ID10001",
},
{
"item": "10002",
"name": "ID10002",
}
]
I would like to add the following into the "itemInfo":
{
"item": "10003",
"name": "ID10003",
}
However, when I run my Java code, it adds this to the end of the JSON file, rather than inserting the new entry following the original 2:
[{"item":"10001","name":"ID10001"},{"item":"10002","name":"ID10002"},{"item":10003,"name":"ID10003"}]
Thanks in advance for any advice you may offer!
I run this code and it is working fine can you test this stuff on your side. If i understand you question correct.
public static void main(String args[]) throws Exception {
{
File file = new File("jsonFormatting.json");
if (!file.exists()) {
System.out.println("No file");
} else {
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("jsonFormatting.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray jsonItemInfo = (JSONArray) jsonObject.get("itemInfo");
JSONObject newObject = new JSONObject();
newObject.put("item", new Integer(10003));
newObject.put("name", "ID10003");
StringWriter out = new StringWriter();
newObject.writeJSONString(out);
String jsonText = out.toString();
System.out.println(jsonText);
jsonItemInfo.add(newObject);
jsonObject.put("itemInfo", jsonItemInfo);
FileWriter fileToWrite = new FileWriter("jsonFormatting.json", false);
try {
fileToWrite.write(jsonObject.toJSONString());
} catch (IOException e) {
e.printStackTrace();
}
fileToWrite.flush();
fileToWrite.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
my jsonFormatting.json file data look like
{"sampleArray": [
"Element_0",
"Element_1"
],
"dataPoint_1": 40,
"dataPoint_2": 500,
"dataPoint_3": 650,
"itemInfo": [
{
"item": "10001",
"name": "ID10001"
},
{
"item": "10002",
"name": "ID10002"
}
]
}
and output is
{
"sampleArray": [
"Element_0",
"Element_1"
],
"itemInfo": [
{
"item": "10001",
"name": "ID10001"
},
{
"item": "10002",
"name": "ID10002"
},
{
"item": 10003,
"name": "ID10003"
}
],
"dataPoint_2": 500,
"dataPoint_1": 40,
"dataPoint_3": 650
}
{
"Response": {
"MetaInfo": {
"Timestamp": "2011-11-21T14:55:06.556Z"
},
"View": [
{
"_type": "SearchResultsViewType",
"ViewId": 0,
"Result": [
{
"Relevance": 0.56,
"MatchQuality": {
"Country": 1,
"State": 1,
"County": 1,
"City": 1,
"PostalCode": 1
},
"Location": {
"LocationType": "point",
"DisplayPosition": {
"Latitude": 50.1105,
"Longitude": 8.684
},
"MapView": {
"_type": "GeoBoundingBoxType",
"TopLeft": {
"Latitude": 50.1194932,
"Longitude": 8.6699768
},
"BottomRight": {
"Latitude": 50.1015068,
"Longitude": 8.6980232
}
},
"Address": {
"Country": "DEU",
"State": "Hessen",
"County": "Frankfurt am Main",
"City": "Frankfurt am Main",
"District": "Frankfurt am Main",
"PostalCode": "60311",
"AdditionalData": [
{
"value": "Germany",
"key": "CountryName"
}
]
}
}
}
]
}
]
}
}
I am trying to retrieve the postal code from the above JSON. I am using gson to parse it. I am very new to JSON and from what i read from all the posts here(some very similar to this), I understood that the fields name should be as it is. So I understand i have to make 4 classes viz Response, view, Result and Address. I made them static nested classes, but I am only getting null value as output. In the next JSON, I have multiple addresses. But I am stuck on this single response.
For a short example, I try to retrieve Timestamp with this code, but it gives me a null value
public class ParseJSON {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("try.json"));
Gson gson = new GsonBuilder().create();
Pojo pojo = gson.fromJson(br,Pojo.class);
System.out.println(Pojo.Response.MetaInfo.Timestamp);
br.close();
}
}
class Pojo {
public Pojo() { }
static class Response{
static class MetaInfo {
static public String Timestamp;
public String getTimestamp() {
return Timestamp;
}
}
}
}
If you only need the "PostalCode", you could use JsonParser instead of having a bunch of classes:
JsonParser jsonParser = new JsonParser();
JsonObject address = jsonParser.parse(json)
.getAsJsonObject().get("Response")
.getAsJsonObject().getAsJsonArray("View").get(0)
.getAsJsonObject().getAsJsonArray("Result").get(0)
.getAsJsonObject().get("Location")
.getAsJsonObject().getAsJsonObject("Address");
String postalCode = address.get("PostalCode").getAsString();
or for all results:
JsonArray results = jsonParser.parse(json)
.getAsJsonObject().get("Response")
.getAsJsonObject().getAsJsonArray("View").get(0)
.getAsJsonObject().getAsJsonArray("Result");
for (JsonElement result : results) {
JsonObject address = result.getAsJsonObject().get("Location").getAsJsonObject().getAsJsonObject("Address");
String postalCode = address.get("PostalCode").getAsString();
System.out.println(postalCode);
}
To make your Timestamp example work, try:
public class ParseJSON {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("try.json"));
Gson gson = new GsonBuilder().create();
Pojo pojo = gson.fromJson(br, Pojo.class);
System.out.println(pojo.Response.MetaInfo.Timestamp);
br.close();
}
}
class Pojo {
Response Response = new Response();
}
class Response {
MetaInfo MetaInfo = new MetaInfo();
}
class MetaInfo {
String Timestamp;
}