AWS Api gateway response to JSON in .NET Core

623 Views Asked by At

I'm working on a project where i need to consume AWS API gateway end point in my .NET core API. I'm getting response from AWS api but it's in application/json format.

https://hbgamarapi.azurewebsites.net/GetPressValues

There is no method available in AWS SDK to convert the below response in to JSON.

here is my controller code

var _val = string.Empty;

        
            using (var _client = CreateApiClient.CreateClient(DataUrl.AkranesWheightUser))
            {
                using (var _reponse = await _client.GetAsync(DataUrl.WheightAkranes))
                {

                    _val = await _reponse.Content.ReadAsStringAsync();

                var jsonDoc = Document.FromJson(_val);
                var res = jsonDoc.ToJson();
                
                
                }
            }
        
        return _val;

that's the response from AWS api gateway

{"Items": [{"Thyngd": {"N": "16"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1838"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "20/08/19 10:44:40"}}, {"Deild_Nr": {"N": "307"}, "Lota": {"N": "21"}, "Dagur": {"S": "22/06/20 06:21:53"}, "Thyngd": {"N": "33"}, "Tegund": {"S": "Almennt"}, "ID": {"N": "2289"}, "Deild": {"S": "NORDANFISK"}, "FL_Nr": {"N": "500"}}, {"Thyngd": {"N": "200"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1007"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "10/07/18 08:56:01"}}, {"Thyngd": {"N": "63"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1581"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "09/04/19 07:43:16"}}, {"Thyngd": {"N": "237"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1175"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "03/10/18 10:33:58"}}, {"Thyngd": {"N": "341"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1259"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "13/11/18 08:44:09"}}, {"Thyngd": {"N": "8"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1933"}, "Deild": {"S": "Br\u0153\u00f0sla"}, "Dagur": {"S": "25/09/19 9:34:39"}}, {"Thyngd": {"N": "221"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1091"}, "Deild": {"S": "BIAMAR"}, "Dagur": {"S": "20/08/18 08:03:27"}}, {"Thyngd": {"N": "235"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1303"}, "Deild": {"S": "Br\u0153\u00f0sla"}, "Dagur": {"S": "29/11/18 11:36:58"}}, {"Thyngd": {"N": "32"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1892"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "06/09/19 11:19:52"}}, {"Deild_Nr": {"N": "308"}, "Lota": {"N": "21"}, "Dagur": {"S": "20/05/20 08:02:19"}, "Thyngd": {"N": "63"}, "Tegund": {"S": "Almennt"}, "ID": {"N": "2251"},

Please suggest possible method

1

There are 1 best solutions below

0
Michael Wang On BEST ANSWER

You could use Newtonsoft to deserialize object. Based on the date of your request, use ExpandoObjectConverter to convert complex model from JSON.

First, Install Newtonsoft through NuGet.

    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    
    [Route("/GetPressValues")]
    public async Task<dynamic> GetJsonAsync() 
    {
        var _val = string.Empty;

        dynamic pressValues;

        using (var client = _clientFactory.CreateClient())
        {
            using (var _reponse = await client.GetAsync("https://hbgamarapi.azurewebsites.net/GetPressValues"))
            {

                _val = await _reponse.Content.ReadAsStringAsync();
                var converter = new ExpandoObjectConverter();
                
                //use ExpandoObject whose members can be dynamically added
                pressValues = JsonConvert.DeserializeObject<ExpandoObject>(_val, converter);

            }
        }

        return pressValues.Items;//Here you get List of PressValues
    }

Screenshots of test: enter image description here enter image description here