Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want generate json schema from json datafile, like below.(this is generated with online tool, http://jsonschema.net/) is it possible with JSON.NET or any other?

XML
{
  "graph": [
    {
      "id": 453,
      "cid": 143,
      "title": "graph1",
      "description": "",
      "thumbnailPath": "art.jpg",
      "detailPath": "art.jpg",
      "link": "www.test.html",
      "author": "graph Team"
    },
    {
      "id": 12,
      "cid": 121,
      "title": "graph2",
      "description": "",
      "thumbnailPath": "art2.jpg",
      "detailPath": "art2.jpg",
      "link": "www.test2.html",
      "author": "graph Team"
    }
  ]
}


OutPut file:

XML
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "/",
  "type": "object",
  "properties": {
    "graph": {
      "id": "graph",
      "type": "array",
      "items": {
        "id": "1",
        "type": "object",
        "properties": {
          "id": {
            "id": "id",
            "type": "integer"
          },
          "cid": {
            "id": "cid",
            "type": "integer"
          },
          "title": {
            "id": "title",
            "type": "string"
          },
          "description": {
            "id": "description",
            "type": "string"
          },
          "thumbnailPath": {
            "id": "thumbnailPath",
            "type": "string"
          },
          "detailPath": {
            "id": "detailPath",
            "type": "string"
          },
          "link": {
            "id": "link",
            "type": "string"
          },
          "author": {
            "id": "author",
            "type": "string"
          }
        }
      }
    }
  }
}



my goal is not using a tool because i want generate it on runtime in program.
Posted
Updated 29-Aug-17 3:54am
v2

1 solution

Please see: https://sixgun.wordpress.com/2012/02/09/using-json-net-to-generate-jsonschema[^].

You should understand that generating metadata from data can be ambiguous; it depends on how representative your data sample is.

[EDIT]

My last paragraph above suggests that this approach may not make a lot of sense; it depends on your goals, which you did not share with us.

I would suggest an alternative approach. You could use JSON only through the Data Contract:
https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer%28v=vs.110%29.aspx[^].

How is that related to the schema? I'll explain. The serializer referenced about, as well as any other serializer based on data contract and most other serializers, is based on reflection.

When, in your comment, you ask about the "class", it cannot be always the class; generally, this is a set of related types, so their instances form one linked data graph. The serialization is the tool which gives you the way to persist this graph in a stream and later restore from the stream as it is.

Are you getting the idea?

You need to use reflection on the data classes making a contract, to translate the contract structure into the equivalent JSON schema form. Compared to the fully-fledged serializer, which you already have from .NET FCL, this is a relatively simple task. Why? Because "real" serializer uses System.Reflection.Emit to generate a serialization assembly, to reuse serialization code, for performance reasons. Normally, you serialize the schema (metadata) much more rarely than the data, so you call allow yourself to ignore the performance (which also would be good enough; because schema cannot big too big to present a problem, but data can be). Use just System.Reflectionm without emit and just generate schema JSON string from .NET metadata.

—SA
 
Share this answer
 
v2
Comments
eagers 17-May-15 14:38pm    
thanks Sergey Alexandrovich Kryukov for answer, in that link the technique is use the existance class to generate schema, but my goal is generate it from a data file. by the way thanks.
Sergey Alexandrovich Kryukov 17-May-15 14:42pm    
Sorry, I don't see the big difference.
—SA
Sascha Lefèvre 17-May-15 14:50pm    
Sergey, I think you either misunderstood his question or posted a different link than you intended to. The link doesn't address his problem.
/Sascha
Sergey Alexandrovich Kryukov 17-May-15 14:55pm    
Maybe I misunderstood the material in my link, but I think I understood the question.
Generally, I have somewhat negative view on the idea of generation of metadata from data samples.

Now, please see my alternative suggestion, after [EDIT].

—SA
eagers 17-May-15 14:56pm    
i work on a program that in runtime generate a class for Json data files. so only a data file exist, my plan is STEP1: generate a json schema. STEP2: generate some classes for it in runtime, so i think this link cant help me, but if u see something that i can't, please explain it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900