cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Custom Snap- Get response in json format

DharaSP
New Contributor

I am developing a custom SNAP that makes HTTP request and gets JSON response in return. The response is array of objects. In order to right it to document , currently it only shows up if I convert the response to string in following manner:

Map<String, Object> data = new LinkedHashMap<>();
data.put(โ€œResponseโ€, response);
outputViews.write(documentUtility.newDocumentFor(document, data));

But Iโ€™d like to instead preview the response in standard json format in outputview. Any idea how this can be achieved ? Any help would be appreciated . Thank you!

1 ACCEPTED SOLUTION

ptaylor
Employee
Employee

Yes, youโ€™ll want to use the Jackson libraryโ€™s ObjectMapper class for that.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
...
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final TypeReference<Object> TYPE_REF = new TypeReference<Object>() {};
...
Object parsedResponse = OBJECT_MAPPER.readValue(response, TYPE_REF);

Note there are other versions of the readValue method that take a Reader or InputStream, which means you might be able to avoid reading the entire response as a String.

Hope that helps.

View solution in original post

21 REPLIES 21

DharaSP
New Contributor

@mbowen Thank you for pointing to that directions- clicking on snap gives following console error:

Screen Shot 2021-08-25 at 3.38.03 PM

DharaSP
New Contributor

I have never seen this error before. Any idea what am I missing ?
Any help would be appreciated.

@ptaylor I made some functional changes without changing anything in pom file.
No updates are done to version metadata. I also tried to include
SingleDocGenerator inside my custom snappack to see of the problem exists with the snaps or snappack and I could not get UI show up for SingleDocGenerator too.

Deployed the new snappack by uploading zip file from Manager UI

I am seeing following error in console.

Screen Shot 2021-08-25 at 3.59.33 PM

You uploaded the snap pack zip file to your organizationโ€™s root shared project?

Can you please find that snap pack in the Manager and click on the snap pack name? What do you see?