0

I'm a user for libarry Json.NET Schema, is there a function for generate json sample by given the json schema?

Shawn
  • 702
  • 1
  • 9
  • 36
  • I don't believe this is currently built into the Json.NET schema. Related: [Generate sample Json output from Json Schema](https://stackoverflow.com/q/21894873), [Generate smaple JSON from JSON schema](https://stackoverflow.com/q/26181340). – dbc Sep 09 '20 at 16:16

1 Answers1

0

I finally tried another way to get the result, refer the function:

    /// <summary>
    /// Generate a random Json sample based on json schema
    /// </summary>
    /// <param name="schema"></param>
    /// <returns>a random Json sample</returns>
    public static JToken GenerateJsonSample(this JSchema schema)
    {
        JToken output;
        switch (schema.Type)
        {
            case JSchemaType.Object:
                var jObject = new JObject();
                if (schema.Properties != null)
                {
                    foreach (var prop in schema.Properties)
                    {
                        jObject.Add(LowerCaseFirstChar(prop.Key), GenerateJsonSample(prop.Value));
                    }
                }

                output = jObject;
                break;
            case JSchemaType.Object | JSchemaType.Null:
                var jObject2 = new JObject();
                if (schema.Properties != null)
                {
                    foreach (var prop in schema.Properties)
                    {
                        jObject2.Add(LowerCaseFirstChar(prop.Key), GenerateJsonSample(prop.Value));
                    }
                }

                output = jObject2;
                break;
            case JSchemaType.Array:
                var jArray = new JArray();
                foreach (var item in schema.Items)
                {
                    jArray.Add(GenerateJsonSample(item));
                }

                output = jArray;
                break;
            case JSchemaType.Array | JSchemaType.Null:
                var jArray2 = new JArray();
                foreach (var item in schema.Items)
                {
                    jArray2.Add(GenerateJsonSample(item));
                }

                output = jArray2;
                break;

            case JSchemaType.String:
                output = new JValue("string_sample");
                break;
            case JSchemaType.String | JSchemaType.Null:
                output = new JValue("nullable_string_sample");
                break;
            case JSchemaType.Number:
                output = new JValue(1.0);
                break;
            case JSchemaType.Integer:
                output = new JValue(1);
                break;
            case JSchemaType.Boolean:
                output = new JValue(false);
                break;
            case JSchemaType.Null:
                output = JValue.CreateNull();
                break;

            default:
                output = null;
                break;
        }

        return output;
    }

    private static string LowerCaseFirstChar(string name)
    {
        return name.Substring(0, 1).ToLower() + name.Substring(1);
    }

then, your calling code would be:

var sampleJsonContent = yourJSchema.GenerateJsonSample().ToString();
Shawn
  • 702
  • 1
  • 9
  • 36