2

I am hoping this is me not holding it right, but I am trying to generate a JSON schema from a JSON string that I have in memory.

I am using the NJsonSchema off of GitHub. https://github.com/NJsonSchema/NJsonSchema/

string json = @"{
  ""a"": 1,
  ""name"": ""Bill Smith"",
  ""isTall"": true
}";

var schemaItem = JsonSchema4.FromJson(json);
string jsonSchema = schemaItem.ToJson();

When I execute the code above I get the following value in jsonSchema which is not correct.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "a": 1,
  "name": "Bill Smith",
  "isTall": true
}

I have created a GitHub bug for it for the time being just in case. https://github.com/NJsonSchema/NJsonSchema/issues/180

halfer
  • 19,824
  • 17
  • 99
  • 186
Jerrod Horton
  • 1,605
  • 1
  • 15
  • 30

2 Answers2

3

In the current version of NJsonSchema you can generate a JSON Schema from sample data:

See https://github.com/RSuter/NJsonSchema/wiki/SampleJsonSchemaGenerator

The commit: https://github.com/NJsonSchema/NJsonSchema/commit/28d5a1357a9b5a3367154d3bc6e2488769b721aa

Will be available in version v7.6.

The DataToJsonSchemaGenerator: https://github.com/NJsonSchema/NJsonSchema/blob/master/src/NJsonSchema/Generation/DataToJsonSchemaGenerator.cs

I think there's also a FromJsonData method on JsonSchema4....

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
1

You are using it wrong, JsonSchema4.FromJson(string json) is used to load existing schema json, not loading arbitrary json and generating a schema from it.

What you might be looking for is JsonSchema4.FromType<MyType>() which can generate a schema from a type, though you would need to have actual types for everything then.

As far as I can tell there isn't any way to generate a schema from just json so you'll need to create a class that matches your input. If you need that then you can have a look at http://www.newtonsoft.com/jsonschema, though it is not a free solution (and I'm in no way affiliated with it).

Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68
  • Thanks for the response. Unfortunately creating types for my json objects is not something I have the opportunity to do with the application for which I am using it. I am looking to do exactly what this does: http://jsonschema.net/#/. I have an object represented in JSON and I want the associated JSON Schema. Also, It seems that JsonSchema4.FromUrl(validUrlWithJson) works how I would expect as well. It takes the JSON from that URL and seems to create a schema from it. – Jerrod Horton Aug 27 '16 at 19:24
  • Just had a look at the source for `JsonSchema4.FromUrl` and all it does it is fetch data from a url and then pass it to `JsonSchema4.FromJson` so it won't help you. I'm afraid that you might need to code this one yourself though it shouldn't be that hard to do since json just have a few types for you to handle (string, int, bool, possibly date). – Karl-Johan Sjögren Aug 27 '16 at 21:16
  • Yeah thanks. I was hoping something would do it for me so I didn't have to run the risk of meaaibg up the schema from hand building it. – Jerrod Horton Aug 28 '16 at 13:15