2

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?

Json DataFile :

{
  "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:

{
  "$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.

1

1 Answer 1

1

Ok I did this:

  1. download the visual studio extension https://visualstudiogallery.msdn.microsoft.com/b4515ef8-a518-41ca-b48c-bb1fd4e6faf7
  2. rename the .vsix file to .zip
  3. extract the zip file to arbitrary folder
  4. reference the Microsoft.Json.SchemaExtension.dll and Microsoft.Json.SchemaProducer.dll
  5. make sure your project is beeing a .net framework 4.5 project (as these 2 assemblies are build with .net 4.5, I didnt find those assemblies elsewhere on the net)
  6. use this code to get the JSON schema from your JSON
  7. though I can not guarantee to you that this approach is legal

-

string json = @"{
  ""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""
    }
  ]
}";
string jsonSchema = Microsoft.Json.SchemaProducer.SchemaBuilder.CreateSchema(json);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer, but in your part I of your solution ther is a code that we haven't it. (before string schemaJson=...) , in theory we only have a well known json file and we want get it Json Schema.
Thanks @Michal Hainc, i think your methood is a soloution, but unfortunately i work on Xamarin and my project use .NET 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.