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

You donโ€™t need to add parsedResponse to a Map unless for some reason you need the output structured that way. You can just directly use parsedResponse as the output documentโ€™s data:

outputViews.write(documentUtility.newDocumentFor(document, parsedResponse));

DharaSP
New Contributor

Your solution worked like a charm!! Thank you @ptaylor

DharaSP
New Contributor

After making few changes, I deployed my snap pack and suddenly do not see property UI for any of the snaps. I tried clicking on snaps multiple times but no luck. Can someone please help as to what must be going on there ?

Can you please provide more details about what you are seeing?

What sort of changes did you make?

Did you make any updates to the version metadata?

How did you deploy the new snap pack?

@DharaSP the web console (right-click inspect โ€ฆ) may also shed some clues โ€ฆ