Forum Discussion

JiWon's avatar
JiWon
New Contributor II
6 years ago

Get Ouptviews for a custom snap

Hi Experts,

I am developing a custom snap which connects to a Mapper. In process(Document document, String s), I get data from the Mapper and then I convert this map data to JSON object by:
Map<String, Object> inputDataFromMapper = document.get(Map.class);.
JSONObject inputDataJsonObj = new JSONObject(inputDataFromMapper);

And now I want to show this JSON object as the outputview of my custom snap by:
Document data = documentUtility.newDocument(documentUtility.newDocument(inputDataJsonObj), document);
outputViews.write(documentUtility.newDocumentFor(document, data));

However, I am getting this error in my custom snap after validating the pipeline.

Please share your ideas on how I can solve this error!

3 Replies

  • Hi JiWon,

    There’s no need to use the JSONObject class or anything similar to convert the map data to JSON. In the Snap API, the data structures associated with input and output documents aren’t JSON; they are just Java objects like Map, List, and primitives (String, Integer, etc). These data structures can be easily mapped to JSON but you don’t need to use any sort of JSON-specific API to read or write them.

    Try something like this instead:

    Document outputDoc = documentUtility.newDocumentFor(document, inputDataFromMapper);
    outputViews.write(outputDoc);
    
    • JiWon's avatar
      JiWon
      New Contributor II

      Hi ptaylor,

      Thanks for being a big help! It works

  • DharaSP's avatar
    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. Any idea how this can be achieved ? Any help would be appreciated . Thank you!