How to Generate JSON from JSON Schema File in .Net C#?
Many a times we may have the JSON Schema file describing the JSON content type and its format. Our requirement may be to generate a JSON file from the Schema with the default values for the properties populated from the Schema file. Here we are going to see how this can be done using a simple static utility class in .Net.
Generating Default JSON from JSON Schema file
For this example I have created a Console Application in C# .Net Framework, and in the Program.cs file I have hardcoded the JSON Schema which acts as our input to generate the JSON.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | using Newtonsoft.Json.Linq; using Newtonsoft.Json.Schema; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace JSONGeneratorFromSchema { public static class JSONGenerator { public static JToken Generate(JSchema schema) { JToken token = null; switch (schema.Type) { case JSchemaType.None: break; case JSchemaType.Object: { var jObj = new JObject(); if (schema.Properties != null) { foreach (var property in schema.Properties) { jObj.Add(Transform(property.Key), Generate(property.Value)); } } return jObj; } break; case JSchemaType.Array: { var array = new JArray(); foreach (var item in schema.Items) { array.Add(Generate(item)); } return array; } break; case JSchemaType.Integer: { if(schema.Default != null) { var value = new JValue(int.Parse(schema.Default.ToString())); return value; } } break; case JSchemaType.Number: { if (schema.Default != null) { var value = new JValue(float.Parse(schema.Default.ToString())); return value; } } break; case JSchemaType.String: if (schema.Default != null) { var value = new JValue(schema.Default.ToString()); return value; } break; case JSchemaType.Boolean: if (schema.Default != null) { var value = new JValue(bool.Parse(schema.Default.ToString())); return value; } break; case JSchemaType.Null: return null; break; default: break; } return token; } private static string Transform(string name) { return name.Substring(0, 1).ToLower() + name.Substring(1); } } internal class Program { static void Main(string[] args) { string schema = @" { ""type"": ""object"", ""properties"": { ""countries"": { ""type"": ""array"", ""items"": { ""type"": ""string"" } }, ""address"": { ""type"": ""object"", ""properties"": { ""HouseNo"":{ ""type"":""number"", ""default"" : 100 }, ""Street"":{ ""type"":""string"", ""default"" : """" }, ""city"":{ ""type"":""string"", ""default"" : """" }, ""Country"":{ ""type"":""string"", ""default"" : """" } } } }, }"; var parsedSchema = JSchema.Parse(schema); var jsonDocument = JSONGenerator.Generate(parsedSchema); Console.WriteLine(jsonDocument.ToString()); Console.ReadKey(); } } } |
Output obtained as a result of executing the above code is shown below:
As you can see that the values assigned to the properties are the default ones specified in the JSON Schema file.
Comments
Post a Comment