Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Mongoose schema from JSON

I am new into mongodb, nodejs and mongooseJS. Lately, I have been trying to create a mongoose schema for my JSON.

{
  "endpoints":["a","z"],
  "poi":[{
  "location_name": "a",
  "latitude": " 10.1075702",
  "longitude": "76.345662",
  "distance" : "0.0"
}, {
  "location_name": "b",
  "latitude": "10.110199",
  "longitude": "76.3489361",
  "distance" : "2.0"
}, {
  "location_name": "c",
  "latitude": "10.1197471",
  "longitude": "76.342873",
   "distance" : "3.1"
}, {
  "location_name": "d",
  "latitude": "10.1254479",
  "longitude": "76.3332626",
   "distance" : "4.4"
}, {
  "location_name": "e",
  "latitude": "10.1443277",
  "longitude": "76.2566017",
  "distance" : "13.9"
}, {
  "location_name": "f",
  "latitude": "10.1487145",
  "longitude": "76.2441114",
   "distance" : "15"
}, {
  "location_name": "z",
  "latitude": "10.145578",
  "longitude": "76.2317077",
  "distance" : "16.9"
}]
}

This is my JSON file that I have. I tried using generate-schema from https://github.com/nijikokun/generate-schema which gave me the following output

 { 
endpoints:[ 'String' ], 
poi: [ 'String' ]
 }

I used this and when I tested it using Postman from chrome webstore, I was not able to retrieve the complete JSON from the database using the get request. Neither I was able to run a post request successfully.

Recently I tried using JSON schema instead of the mongoose schema using

mongoose.Schema("JSON Schema')

When I try using JSON Schema I am able to retrieve the data from the mongodb collections using the GET request but I'm not able to POST data correctly with the JSON Schema

I was also thinking about dropping nodejs and redeveloping the webservice in java and mongodb. If I try to use Java web service for interacting with mongodb, is it going to affect the performance of my web app?

like image 389
persistent_poltergeist Avatar asked Sep 07 '16 05:09

persistent_poltergeist


People also ask

Is there a schema for JSON?

JSON Schema is an IETF standard providing a format for what JSON data is required for a given application and how to interact with it. Applying such standards for a JSON document lets you enforce consistency and data validity across similar JSON data.

How do I create a schema in MongoDB?

As we already know MongoDB is schema-less, at the time of creating any objects we cannot create any schema in MongoDB. We can enforce the schema for collection in MongoDB by using the MongoDB atlas cluster, for enforcing the document schema we need first to connect the database and collection.

How do I create a JSON Schema?

You can Drag and drop a JSON file, click on "Browse a JSON file" or directly type in the editor. Step 2: You can choose the options (below the editor). If you check "Only required" then only the required fields will be filled in the generated JSON. Step 3: Click on "Generate JSON from Schema" button.


1 Answers

You can use Generate Schemas module to do this task.

var jsonObject={
var GenerateSchema = require('generate-schema')
var schema = GenerateSchema.json('Product',jsonObject);

console.log(JSON.stringify(schema))

Since you have two main properties one is endpoints and other poi

And here is the output schema of your JSON object

    {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Product",
  "type": "object",
  "properties": {
    "endpoints": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "poi": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "location_name": {
            "type": "string"
          },
          "latitude": {
            "type": "string"
          },
          "longitude": {
            "type": "string"
          },
          "distance": {
            "type": "string"
          }
        }
      }
    }
  }
}

Suggestion: You will get some unwanted field and you have to modify it. So I think you should create custom schema on the basis of your object, which would be better for you

You can also get other references here

like image 144
abdulbarik Avatar answered Sep 28 '22 02:09

abdulbarik