0

I would like to be able to dynamically create valid reference data using a JSON schema, I'm not sure if there is something built into NewtonSoft.JSON for this or not, or if there is some combination of classes and methods I could use to easily generate the appropriate JSON.

I've been tinkering with this over the past few days and this is what I have thus far.

CreateJson

static void CreateJson(KeyValuePair<string, JSchema> jsonProperty, JObject jObject)
{
    switch (jsonProperty.Value.Type)
    {
        case JSchemaType.Array:
            jObject.Add(new JProperty(jsonProperty.Key, new JArray()));
            foreach (var jItem in jsonProperty.Value.Items)
            {
                foreach (var jProp in jItem.Properties)
                {
                    CreateJson(jProp, jObject);
                }
            }
            break;
        case JSchemaType.Object:
            JObject nestedObject = new JObject();
            foreach (var jProp in jsonProperty.Value.Properties)
            {
                CreateJson(jProp, nestedObject);
            }
            jObject.Add(new JProperty(jsonProperty.Key, nestedObject));
            break;
        default:
            jObject.Add(new JProperty(jsonProperty.Key, jsonProperty.Value.Default));
            break;
    }
}

It gets called like this

example

JSchema schema = JSchema.Parse(jsonSchema);

JObject jsonObject = new JObject();

foreach (var prop in schema.Properties)
{
    CreateJson(prop, jsonObject);

}

The output is a little wonky though

{{
  "checked": false,
  "dimensions": {
    "width": 0,
    "height": 0
  },
  "id": 0,
  "name": "",
  "price": 0,
  "tags": []
}}

First it appears there is the double braces, which I'm not 100% certain where they came from. The next is that Dimensions is an object with Width and Height as properties, they are not nested inside the Dimensions object. For this POC I know exactly what and where they should be, but in production the code won't "know" anything about the schema coming in. I feel I'll have a similar problem with the array, but not sure that actually worked as the array is just strings, and I was more interested in the object.

I was looking at this example on Newtonsoft.com but again that sample knows exactly where the thing is they want to find. I am almost wondering if when the code encounters an object it should handle it differently...or perhaps the default switch that is capturing the individual properties is wrong.

Any advice is again very much appreciated.

9
  • There is nothing built into Newtonsoft for this. Json.NET schema can load schemas, validate JSON, create schemas from scratch, or generate them from .NET types. It has no test JSON generation capabilities. Commented Apr 16, 2021 at 19:34
  • 1
    You could look at Generate sample Json output from Json Schema, C# library for converting json schema to sample JSON. Commented Apr 16, 2021 at 19:39
  • 1
    github.com/RicoSuter/NJsonSchema has a method ToSampleJson(), maybe that will do what you want. Commented Apr 16, 2021 at 19:40
  • Thanks! I've seen that first SO article, but I've not run across the second, and that repo, I'll dig into that as well, appreciate your time! Commented Apr 16, 2021 at 21:16
  • I feel I've made some progress in what I'm after, but I'm getting some odd output, updating the original post with code Commented Apr 19, 2021 at 3:40

0

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.