0

New to mongodb and this is more a convention question. I have a python server on the backend that retrieves documents from mongodb and serves as REST API. The API returns data after serializing it using the bson.json_util library.

return bson.json_util.dumps(response, default=default)

This serialized json has an extra layer for certain data types, such as $oid for unique ids, $date for dates and so on. My question is whether there is a javascript deserializer that takes the json and converts it into an object ? Otherwise the data needs to be accessed with an extra layer (start_time.$date). Or is the convention to strip this layer in the API implementation ?

Sample data after being serialized.

{
    "name": "test_name_0",
    "start_time": {
      "$date": 1493650775227
    },
    "_id": {
      "$oid": "59074d577653512111045b3f"
    },
    "suite_id": {
      "$oid": "59074d577653512111045b3e"
    }
}
Dan Green-Leipciger
  • 3,776
  • 1
  • 19
  • 29
gangof4
  • 85
  • 7
  • Could you post a snippet of how your data looks when it comes out of the serializer – Dan Green-Leipciger May 11 '17 at 17:32
  • added an example data after being serialized. Thanks – gangof4 May 15 '17 at 05:45
  • Could you also post what you would like the data to ultimately look like. If that is your question, what should the data look like, please clarify that and I will answer accordingly. – Dan Green-Leipciger May 15 '17 at 13:49
  • I guess the question is if there is a javascript deserializer for this type of json. That is, in order to access start_time, we would need to access it with the data path, json["start_time"]["$date"]. Instead, is there a way to deserialize it so a $date value turns into a Date() object that can be accessed via deserialized_json.start_time. I'm assume elements like $date, $oid are there to specify the data type, so it would make sense if a deserializer already existed to make use of that info. – gangof4 May 15 '17 at 15:44

0 Answers0