1

I'm trying to get JSON Schema from a JSON entity using Newtonsoft JSON.Net.

This works as expected:

dynamic customObject = new
{
    Title = "Test"
};
var schemaGenerator = new JsonSchemaGenerator { };
var schema = schemaGenerator.Generate(customObject.GetType());

This works fine. The .Properties property of the schema shows the 'Title' property with details.

However my source for the schema is a piece of JSON data. So I first serialize it to dynamic:

string json = JsonConvert.SerializeObject(customObject);
dynamic customObjectAfterSerialize = JsonConvert.DeserializeObject<dynamic>(json);

JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator { };
JsonSchema schemaAfterSerialize = schemaGenerator.Generate(customObjectAfterSerialize.GetType());

But now the .Properties property of the JSON schema object is null.

How can I get a JSON schema from an arbitrary piece of JSON data?

1
  • In the first call to Generate you are passing a true dynamic object but in the second one you are passing a JObject. Not sure how to fix it but that may help you find a fix.
    – Aboo
    Jan 5, 2016 at 0:39

1 Answer 1

0

dynamic in your first example is a reference to an anonymous typed object.

dynamic in your second example is untyped.

It isn't possible to generate a schema from an untyped object.

1
  • An idea to resolve this request ? How to transform a json to anonymous typed object compatible with the SchemaGenerator ?
    – Julien
    Aug 7, 2020 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.