Forum Discussion

DharaSP's avatar
DharaSP
New Contributor
5 years ago
Solved

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 th...
  • ptaylor's avatar
    5 years ago

    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.