Using JSON.parse on the output of BsonDocument.ToJson results in an error

When fetching information from MongoDB and sending it to the client, I encountered a dilemma:

var retrievedBsonDocument = ... retrieve data from database ...
var dtoObject = new Dto { MyBson = retrievedBsonDocument.ToJson() };

While trying to parse the MyBson property on the client side using JSON.parse, I encountered an error message: SyntaxError: Unexpected token N. This seems to be due to one of the properties being structured like this:

{ ..., "SomeIntProp" : NumberLong(70) }

The JavaScript parser is unable to interpret the Bson data type known as NumberLong.

What would be the best way to convert the BsonDocument to JSON in order to exclude occurrences of NumberLong?

Answer №1

Finding a solution to this problem was quite challenging, but I managed to come up with one by developing my own parsing function tailored to MongoDB BSON types. Unlike the native JSON.parse method which only recognizes JavaScript types, my custom function is able to handle conversions effectively. For those interested, you can access my approach here: https://gist.github.com/UniqueSolutionCreator/1234567 The inclusion of html snippets in the code may not be necessary for your specific use case.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for resolving the error message "TypeError: Converting circular structure to JSON"

I had a straightforward query where I needed to select all from the aliases table. Everything was working fine until I ran npm update. TypeError: Converting circular structure to JSON public async fetchAliases(req: Request, res: Response): Promise< ...

Bookshelfjs fails to return a promise when updating multiple data

I am facing an issue when trying to update multiple rows in my database. The problem lies in the fact that the promise does not return anything, even though the data is successfully saved. Below is a snippet of my code: doUpdate: Promise.method(function ...

Finding the Client's Private IP Address in React or Node.js: A Comprehensive Guide

Issue I am currently facing the challenge of comparing the user's private IP with the certificate's IP. Is there a method available to retrieve the user's private IP in react or node? Attempted Solution After attempting to find the user&a ...

Retrieving Date and Time details from a given text

In my database, there is a wealth of data that includes a date and time string, such as Tue, 21 Sep 2010 14:16:17 +0000 My goal is to retrieve various documents (records) from the database based on the time specified within the date string, Tue, 21 Sep 20 ...

Converting JSON data to PHP format

I am currently working with some JSON data: { 'total': 4744.134525437842, 'produksiHarian': [14.800870530853988, 15.639301040842536, 16.358413710544085, 16.952318230836113, 17.45055097248538, ...], 'r_squared': 0.9 ...

I'm facing an issue with libmono.so on the destination machine after using mkbundle2 for packaging. What could be the reason

I was under the impression that mkbundle2 was designed to enable a machine without mono installed to run a mono application. However, it does not appear to include libmono.so in the package. I am uncertain if it should be included or not, but the target ma ...

having trouble with developing a dropdown menu using jquery

I'm currently creating a drop-down menu for my website and here is the code I'm using: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html dir="ltr"> <head> <met ...

Can Jquery be used to swap out specific li content?

<div class="widget_ex_attachments"> <ul> <li> <i class="fa fa-file-word-o"></i> <a href="uploads/2014/09/Parellel-universe.docx">Parellel universe</a> </li> ...

JavaScript events are failing to trigger within the content area of an iframe

When attempting to run a given example by double clicking inside the content area of an iframe, the clicks and any JavaScript events are not being fired. Currently, the events can only be triggered by clicking on the border of the iframe, not inside the ...

Querying MongoDb to calculate the time interval between switching on and off alternately

{ metadata:{ eventcode:100 } power:on // the power status can vary between on and off time:1667984669//unix timestamp } My document is structured in a way that indicates whether the power is on or off, and I need to calculate the total number of hours it ...

Error Message: "The Mongor bundle in Laravel cannot find the class 'Mongo'"

Issue: I am encountering an error message Class 'Mongo' not found when utilizing the mongor bundle in Laravel. Can anyone offer insight into what might have caused this problem? Following the installation of mongodb and initiation of the mongodb ...

Angular UI-Grid encountering difficulties in rendering secure HTML content

I'm having trouble displaying server-generated HTML in UI-Grid. Specifically, I want to show HTML content in my column header tooltips, but no matter what I try, the HTML is always encoded. Here's an example to illustrate the issue: var app = an ...

When using jQuery, the value of an input type text field remains constant despite any alerts

My issue involves an input text used to check if the corrected values are being displayed in an alert. However, when I modify a value in the form and check if the updated value appears in the alert box, it still shows the old value. Below is the relevant ...

Error in Next.js: The function (0 , firebase_auth__WEBPACK_IMPORTED_MODULE_1__.onAuthStateChanged) is not defined as a function

Just starting out with Next.js development and currently following a Youtube tutorial on creating a Whatsapp clone using firebase8.9 as the database. I am looking to implement a feature where the app checks if the user is logged in, if so redirect them to ...

Is it possible to effectively iterate through an Angular service request?

Can you help me troubleshoot this code to ensure it runs smoothly? onSubmit() { let pdfData = [ { field_name: 'data.Date', value: this.freshDeskData.date, placeholder: '', page_no: 1, }, ...

Utilize the .net API to upload and replace a file within a specified folder on Google Docs

I am currently attempting to upload a file to a specific folder using the .net client for google-api. Here is how I am currently achieving this: service = new DocumentsService("SRDUploader"); DocumentEntry lastUploadEntry = service.UploadDocument(file, nu ...

Performing subtraction on two arrays in Javascript

Is there a way to eliminate all duplicate elements from one array list that are also present in another array list using the .filter() method of ES6 javascript? I am open to solutions in jQuery as well, but they must be compatible with IE11+ and Chrome fo ...

Tips for accessing and storing a particular value from a JSON object array in a variable

I have some code that retrieves data from my database. The output is in the form of a list: results = [(2, '4'), (2, '17'), (3, '65'), (1, '54'), (2, '14'),...] I aim to convert this list into a JSON obj ...

Triggering a page refresh with Framework7

Currently, I am in the process of developing a compact webapp utilizing the framework7 split-view-panel example. This eases navigation by providing a left-hand navigation bar that, upon clicking, loads a URL in the right-hand panel. However, I have notice ...

Exploring Mongoose: A Guide to Populating Data from a Parent to a Child

I'm currently diving into the world of mongoose, where I have set up 3 collections (User, Post, Comment), each with its own unique schema: User { fullName: String, email: String, } Post { author: {type: mongoose.Schema.Types.ObjectId, required ...