I am creating a method in .Net Core 5 Web API controller and this method will be invoked by different clients with input as in JSON format.
In which, number of objects & properties inside datasource_values
is dynamic. It may vary depends on the client. Now I need to write an API method to extract all property values and store only datasource_values
as a JSON file. File name of the JSON should be the value of source
.
If value
has two source
, then I need to create two JSON files.
I tried creating two model classes and API method to extract the JSON data but I could not achieve the desired result. In .Net Core 5, I am not sure how to achieve this.
Input data structure looks like below.
{
"Name": "APAC",
"value": [
{
"source": "datasource1",
"datasource_values": [
{
"ID": 100,
"Name": "ABCD"
},
{
"ID": 101,
"Name": "EFGH"
}
]
},
{
"source": "datasource2",
"datasource_values": [
{
"No": 1,
"Name": "ABC",
"Amount": 1000
},
{
"No": 2,
"Name": "BBC",
"Amount": 1100
}
]
}
]
}
Model Class:
public class CreateJSON
{
public string Name { get; set; }
public List<JSONData> value { get; set; }
}
public class JSONData
{
public string source { get; set; }
public List<object> datasource1_values { get; set; }
}
API method:
[Route("CreateJSONData")]
[HttpPost]
public ActionResult<int> CreateJSONData([FromBody] CreateJSON ipdata)
{
try
{
string json = JsonSerializer.Serialize(ipdata);
File.WriteAllText(@"c:\input.json", json);
return Ok();
}
catch (Exception ex)
{
return StatusCode(500);
}
}