Custom Snap- Get response in json format

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!

What is response's data type? String?

Yes- The response is of type string

This is the structured response I get when using CORE REST GET SNAP.

But with custom snap- I am unable to get the similar structure. What am I missing?

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.

I’ll try that.
The final step would be to add parsedResponse to Map - something like this:

Map<String, Object> data = new LinkedHashMap<>();
data.put(“Response”, parsedResponse);
outputViews.write(documentUtility.newDocumentFor(document, data)) - right ?

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));

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

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 …

@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

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

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

I am seeing following error in console.

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?

@ptaylor : Yes I have uploaded it to my organization’s root shared project.
I see all the snaps on clicking the snap pack name. These are all the snaps included in the snap pack.

NOTE: Create Account Settings UI shows up fine.

@DharaSP if you close the pipeline containing your snaps in Designer, and then re-open the pipeline are you able to inspect snap properties for any of your snaps ?

@mbowen: Closing the pipeline in Designer and re-opening it again now shows properties UI. But this wasn’t the case earlier ?Is this a recent updated to the application?

@DharaSP
No. I think this is a bug. I’ve recently seen this behavior. Let me chat with others to try to pinpoint the exact problem. At least the heavy hammer closing & re-opening pipeline allows you to access the snap properties. Expect another update.

@mbowen Thank you so much for the prompt reply!