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.

Jeff Patton
  • 551
  • 4
  • 15
  • There is nothing built into Newtonsoft for this. [Json.NET schema](https://www.newtonsoft.com/jsonschema/help/html/Introduction.htm) can load schemas, validate JSON, create schemas from scratch, or generate them from .NET types. It has no test JSON generation capabilities. – dbc Apr 16 '21 at 19:34
  • 1
    You could look at [Generate sample Json output from Json Schema](https://stackoverflow.com/q/21894873), [C# library for converting json schema to sample JSON](https://stackoverflow.com/q/45922832). – dbc Apr 16 '21 at 19:39
  • 1
    https://github.com/RicoSuter/NJsonSchema has a method [`ToSampleJson()`](https://github.com/RicoSuter/NJsonSchema/blob/288eb13e931562264ed9ddd12a83741c67a1ddc4/src/NJsonSchema/JsonSchema.cs#L840), maybe that will do what you want. – dbc Apr 16 '21 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! – Jeff Patton Apr 16 '21 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 – Jeff Patton Apr 19 '21 at 03:40
  • When you say "reference data", do you mean example data? – Relequestual Apr 19 '21 at 09:03
  • Yes, basically build a valid JSON document from the schema – Jeff Patton Apr 19 '21 at 12:00
  • I've made a slight change in my code, and have edited the original post with the change. What I've done for the object is to create a new empty object before the loop over properties, and then pass that nested objhect back into the function to populate the properties before adding it back to the original jsonobject when we leave the switch. – Jeff Patton Apr 19 '21 at 13:06
  • After some additional reading i don't think the extra braces are an issue, it apepars they are an artifact of how debugging works? At any rate, putting this into an API i'm not getting that double brace output. – Jeff Patton Apr 19 '21 at 13:44

0 Answers0