Retrieve JSON attachment and its attributes - java

Background:
I have stored a JSON document (id: 160d7237a5fd00501f4654a6c8b42f38) in a CloudantDB and put an attachment against the document. As you can see that below JSON structure has one attachment (i.e. Rajiv.jpg) associated with it.
I want to retrieve the JSON attachment object (JPEG). Can someone help me how I can transform the below JSON structure to a Java POJO class and retrieve the attachment using the same ? What would be the definition of the POJO class. Thanks in advance.
I want to show the JPEG file in the UI screen via a webservice which will retrive the JPEG from CLoudantDB and pass it to UI.
{
"_id": "160d7237a5fd00501f4654a6c8b42f38",
"_rev": "3-695e52989827cd9e203ff9f506eaf7df",
"Customer_Id": "2",
"_attachments": {
"Rajiv.jpg": {
"content_type": "image/jpeg",
"revpos": 2,
"digest": "md5-/78IzjPUu7nkbVbSXBF2Xg==",
"length": 39712,
"stub": true
}
}
}

The JSON you've got doesn't include the actual attachment - just metadata about it.
From the documentation:
To retrieve an attachment, make a GET request to https://$USERNAME.cloudant.com/$DATABASE/$DOCUMENT_ID/$ATTACHMENT. The body of the response is the raw content of the attachment.
So in this case it sounds like you'd make a request to https://[...].cloudant.com/[...]/160d7237a5fd00501f4654a6c8b42f38/Rajiv.jpg
The documentation seems confusing as to whether the response will actually be JSON with Base64 data, or just the data - but try it and see.
You don't need a POJO to parse the original data shown in your question; whatever parser you use is likely to provide a "dynamic" view allowing you to fetch fields by name: just fetch the _attachments field, and then iterate over the fields within the resulting object.

Related

How to convert a JSON to Java object but with translated values

I have a JSON like below and I want to generate the Java object of the respective class by parsing it. The catch is that I don't want the value for maxtime in that object to be set as {{ Instant.MAX.toString()}}, but it should be its translated value, which means it should be +1000000000-12-31T23:59:59.999999999Z. Is there any standard library to achieve this similar requirement or I will have to write a customized code for this?
{
"key1": "",
"key2": "",
"key3": {
"maxTime": "{{ Instant.MAX.toString()}}",
"anotherKey": "{{MyProjectUtils.getKey()}}"
}
}
In the worst case, I am ready to replace this JSON file with some other type of file but at the end, I want a java object with translated values.
You may create a JavaBean/POJO object from a JSON file. You may adjust the members in POJO Object according to the JSON File.
here is how to create a POJO object from a JSON file.

Sending dynamic fieldnames for RequestBody multipart/form-data in spring

I am currently doing an experiment to build the API which users can submit freely at their own will without restriction, no boundary on field names and the contents as well. And the API would just print out the inputs in a key,value pairs. For the files, I would just print out the whole bytes of the base64 converted of it.
For example, I have the request A like this:
{
"name": "John Doe",
"lucky_number": 75
}
Then the API will get the output:
Hello user, your first key is name and its value is John Doe, your next key is lucky_number and its value is 75. Thank you for using.
Then, I make another request with the fields below. The image actually was filled with actual image/jpg file.
"pokemon": "pikachu",
"image": <File>JFIFw9d1j3f0881ehfce0cn2e0f2
So, it will print out most likely:
Hello user, your first key is pokemon and its value is pikachi, your next key is image and its value is cApovnquGCaisa1b23IAbqxcaiFoPqR==. Thank you for using..
Can this actually be achieved? As for what I have known, I can make such dynamic request if the request body was formatted in JSON file that Spring can easily get it to the Java Object format. But what about this multipart? And as far as I know, I can get the multipart only with the defined fieldname, and this code below most likely can only handle one file with field file, but what if the user actually send 3 files or even more as there's no boundary as the expected behavior?
#PostMapping("/fun", consumes = { "multipart/mixed", "multipart/form-data" }, produces = {"text/plain"}
public String postForFun(#RequestPart("file") MultipartFile file, #RequestBody Object dynamicFields) {
}

How to convert JSON object received in response to REST Client request to a MT940 Swift text file in Java?

How to convert sample JSON given below to a MT940 txt file:
This JSON would be a bad sample to show but hope you get the gist of it...
Just like we have a library in place to parse MT940 strings/txt we also have a library which can help construct a MT940 txt file in Java.
{
"accNumber":"123356",
"openBalInd":"D",
"openBalaDate":"200605",
"curr":"Dollar",
"transactions":[
{
"amount":""434,
"credit/debit":"1000",
"datetime":"20042020"
},
{
"amount":""434,
"credit/debit":"1000",
"datetime":"20042020"}]
}
MT940 is SWIFT message type.
Your input is JSON and the output is MT940 text file.
It is always good to have some java model classes representing your json.
Deserialise json input using Jackson to model you gonna use internally.
Use your own library or some third party like https://www.prowidesoftware.com/resources/SWIFT-writer to convert your internal model to MT940
Serialize the result into text file.

How can I read json file in java, update the values and store it back to same file?

I have a json file as below and now I need to update the pincode value Everytime before using and use the updated json file for processing,how can I achieve the same using Java?
{
"Name":"Ramesh",
"City":"Bangalore",
"Address": {
" Pincode":56010
}
}
Umesh, My suggestion are listed as :
Open the file, read the content
Convert that json to object mapper(Jackson ObjectMapper).
Close the file.
Modify the Mapper Object with pincode as per your business
The convert the object mapper to json string
Open the same file to write the modify json string.

Validate html tags in json file using java - whats the best way?

Is there a java library, java framework, or java example that I could supply a URL, www.test.com/storage/test.json, and validate that HTML elements, specifically HTML opening and closing tags, so I can be sure when changes are made to the json file, the web page ( www.test.com/content) that reads the content will still render?
As for a little background, I can be considered a novice when it comes to java development. I am looking to create a automated script which will be ran via jenkins to validate a json file. So far, I have been able to identify placeholders and verify they are not blank, and/or are only numeric for example. I have even been able to identify any type of links that may sit in the json file and make sure they are valid by using java.net.HttpURLConnection. My next goal is to validate HTML tags are properly formatted.
Thank you
Edit/example of json file:
{"list":[{
"type":"example",
"id":"123",
"body": "<p><i>text</i></p>
It is not clear to me what you want to do, but if your question is about a library to validate HTML elements coming from a string in Java, the two I would suggest are jcabi-w3c validator and the Nu HTML5 Validator.
They should play well in validating your HTML entities.
You can validate body field containing HTML content by adding following to your JSON schema:
{
"body": {
"type": "string",
"media": {
"type": "text/html"
}
}
}

Categories

Resources