Home > Software design >  How to interpret extended json data
How to interpret extended json data

Time:03-18

Description of the problem

I will not write any codes as much of my problem is something linked with the knowledge to interpret some data. I am doing a project and had to use a nonsql database to store data the sampled information from a microcontroller. The chosen db was mondodb. I wrote all the code that stores the info and now i want to exhibit the date on a html page. The problem is that when i do the request using restapi to the mongodb, the json that was stored there comes in the extened json format, i.e.:

  "_id": {
            "$oid": "6230d05dcf81542c5aabc30b"
        },
        "sensor": {
            "$numberDouble": "1"
        }

But it should have come as the data is stored in the db:

  
"{
  _id":  "6230d05dcf81542c5aabc30b",
  "sensor": 1.0
  }     

As you can see, the the json comes with extra information linked to the type of the variable that is stored. But i don't really know how to use that information in javascript. I would just read it for example as json.sensor.$numberDouble if i wanted to get the information about the sensor instead of json.sensor if the json was in the normal way. I don't see much of an use to the extended version. Is something i am doing wrong?

CodePudding user response:

Make sure, that you Parse API response, like-

response.json()

CodePudding user response:

It looks like you're getting back an EJSON response. If you want to send back standard JSON response then that should be set as the Content-Type on the server response.

function(req, res) {
    data = { ... }; // Your data here
    res.setHeader("Content-Type", "application/json");
    res.setBody(JSON.stringify(data));
}

If you don't have control over the server side, then convert the EJSON back to plain JSON using the meteor EJSON library.

  • Related