I’m developing application to generate test data.
I don’t know how it’s called, but I need to change JSON schema from:
{
"id": 1,
"data": "Daniel",
"typeID": 1,
"type": {
"id": 1,
"name": "Name",
"templates": []
}
}
to:
{
"id": 1,
"data": "Daniel",
"typeID": 1,
"type": "Name"
}
This is my controller:
[HttpGet("{data}")]
public async Task<IActionResult> GetOne(string data)
{
Template template =_templateService.GetOne(data);
if (template == null)
return await Task.FromResult(NotFound($"Not Found: {data}"));
return await Task.FromResult(Ok(template));
}
Entities:
public sealed class Type
{
public int ID { get; set; }
public string? Name { get; set; }
public List<Template> Templates { get; set; } = new();
}
public sealed class Template
{
public int ID { get; set; }
public string? Data { get; set; }
public int TypeID { get; set; }
public Type? Type { get; set; }
}
And I am using Newtonsoft.Json
.
I tried using [JsonIngore]
near
public List<Template> Templates { get; set; } = new();
but that doesn’t seem to be working.
2
Answers
You misunderstood the purpose of the
JsonIgnore
attribute as mentioned in the comment. From your question and expected result, you need a type casting from the input data to the expected result.Template
instance to aTemplateOutputModel
instance.Template
instance to theTemplateOutputModel
instance.To use JsonIgnore is a good idea, but you you will have to add an extra string property too