0

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
   }   
 }
agentpx
  • 1,051
  • 1
  • 8
  • 24
  • Does this purely have to happen on the client side? ie. using javascript? or do/would you have server side code involved? and what language? – Johan Oct 01 '15 at 08:16
  • I am not validating, I am parsing. Thanks – agentpx Oct 01 '15 at 08:16
  • Realized, thus I deleted it :) – Johan Oct 01 '15 at 08:17
  • 1
    Anyhow, have a look at: http://stackoverflow.com/questions/7341537/tool-to-generate-json-schema-from-json-data – Johan Oct 01 '15 at 08:18
  • Yes on client side using javascript – agentpx Oct 01 '15 at 08:18
  • I literally want to convert the said json object into this format – agentpx Oct 01 '15 at 08:19
  • { "$schema": "http://json-schema.org/draft-04/schema", "title": "Basic Info", "type": "object", "properties": { "firstName": { "type": "string" }, "age": { "type": "number" } } } – agentpx Oct 01 '15 at 08:19
  • 1
    Go to this page http://json-schema.org/implementations.html, go down to the bottom and click on 'Other'. I think that may be what you are looking for. Presumably you want some kind of automatic converter. – mwarren Oct 01 '15 at 08:38
  • Thanks I'll try http://orderly-json.org/ if it will help solving my problem. – agentpx Oct 01 '15 at 12:12

0 Answers0