Tech Learner
Tech Learner

Reputation: 1317

How to convert API method's input parameter to JSON file in .Net Core 5 API?

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);
            }
        }

Upvotes: 0

Views: 1201

Answers (1)

Serge
Serge

Reputation: 43870

You do not need ANY custom classes.

[HttpPost("CreateJSONData")]
public ActionResult<int> CreateJSONData([FromBody] JsonDocument ipdata)
        {
            try
            {
                string json = ipdata.RootElement.ToString();
                File.WriteAllText(@"c:\input.json", json);
                return Ok();
            }
            catch (Exception ex)
            {
                return StatusCode(500);
            }
        }

but i recommend you to use Newtonsoft.Json.

using Newtonsoft.Json;

[HttpPost("CreateJSONData")]
public ActionResult<int> CreateJSONData([FromBody] JObject ipdata)
        {
            try
            {
                string json = ipdata.ToString();
                File.WriteAllText(@"c:\input.json", json);
                return Ok();
            }
            catch (Exception ex)
            {
                return StatusCode(500);
            }
        }

Update

if you need to store only data part

using Newtonsoft.Json;

json = ipdata["value"].ToString();

//or 

using System.Text.Json;

var json= ipdata.RootElement.GetProperty("value").ToString();
....

result

[
  {
    "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
      }
    ]
  }
]

Upvotes: 1

Related Questions