3

I'm using MongoDB Stitch to create a data enabled API, but when I make a GET request, the data is returned where numbers are displayed as:

"firstHit": {
   "$numberInt": "3"

Where I would like them to be return just as:

"firstHit": 3

I have a lot of objects within objects, and I am inserting the data through the mongo shell, I'm not sure of that is of any importance.

Anyone have any experience with this? Thank you!

ClaytonO
  • 31
  • 2
  • Not had much experience with Stitch but another answer [here](https://stackoverflow.com/a/53480188/399435) suggests you are getting back an EJSON response. See this [link](https://stackoverflow.com/a/55362205/399435) for some possible suggestions on how to parse the response. If not please edit your question and add how you are making the GET request. – Karthic Raghupathi May 20 '20 at 22:44

1 Answers1

3

By default, the result format returned by MongoDB Stitch webhooks is in MongoDB Extended JSON format, or EJSON for short. This is useful to define data types that would otherwise be lost in normal JSON. There are some object types that have no equivalent in JSON, for example ObjectId() and Date().

If you would like to return as a normal JSON, you could set the response object as an example below:

exports = function(payload, response) {

    result = {"firsthit": 10};

    response.setStatusCode(200);
    response.setHeader("Content-Type", "application/json");
    response.setBody(JSON.stringify(result));
}

You may also find EJSON library and Stitch Utility Packages as useful additional information.

Wan B.
  • 18,367
  • 4
  • 54
  • 71