Retrieve JSON attachment and its attributes

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
    }
  }
}
Jon Skeet
people
quotationmark

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.

people

See more on this question at Stackoverflow