How to create a json-schema out of a json object

2.1k views Asked by At

Given a browser debugger output of root.value with two properties in javascript

root.value
__proto__: {...}
firstname: "my name"
age: 25

I want to parse it to a JSON string including the type like below. Or literally convert the above json object to the format below.

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "title": "Basic Info",
  "type": "object",
  "properties": {
       "firstName": {
           "type": "string"
         },
         "age": {
           "type": "number"
     }
   }
 }

Does any one know how to do that in javascript or any framework I can use to achieve such?

Note: I did not created the JSON myself it's an output of another framework. So the types of the fields are unknown until runtime.

My main concern is to embed the json object values for

{ "properties": {
      "firstName": {
         "type": "string"
       },
       "age": {
         "type": "number"
       }
}


 JSON.stringify(root.value);

will only return

{
   {
       "firstname":" my name"
   },
   {
        "age": 25
   }   
 }
0

There are 0 answers