Home > Back-end >  Filter JSON response from an API call
Filter JSON response from an API call

Time:05-25

Basically I am making an api call I receive the api call in a JSON format and I need to basically filter through the api call to only return the value's for sys_name. I am unsure how to accomplish this as my JsonConvert.DeserializeObject<>() is coming back blank.

        {
            try
            {
                using (HttpClient client = GetClient())
                {
                    var response = client.GetAsync("this is the api call but I cleared it as you wont be able to use anyway");
                    response.Wait();
                    if (response.Result.IsSuccessStatusCode)
                    {
                        var result = await response.Result.Content.ReadAsStringAsync();
                        Result items = JsonConvert.DeserializeObject<Result>(result);
                        Console.WriteLine(items.SysName);
                        
                        
                        return null;
                    }
                };
            } catch (Exception ex)
            {
                _logger.LogWarning(ex, $"Failed to retrieve requested Table |{ex.Message}");
                Console.WriteLine(ex.Message);
            }
            return null;
            
        }

and then I have a wrapper class that I made using jsonutils.com

    {

        [JsonProperty("link")]
        public string Link { get; set; }

        [JsonProperty("value")]
        public string Value { get; set; }
    }

    public class SysPackage
    {

        [JsonProperty("link")]
        public string Link { get; set; }

        [JsonProperty("value")]
        public string Value { get; set; }
    }

    public class SysScope
    {

        [JsonProperty("link")]
        public string Link { get; set; }

        [JsonProperty("value")]
        public string Value { get; set; }
    }

    public class Result
    {

        [JsonProperty("rec_misc")]
        public string RecMisc { get; set; }

        [JsonProperty("question")]
        public Question Question { get; set; }

        [JsonProperty("sys_mod_count")]
        public string SysModCount { get; set; }

        [JsonProperty("sys_updated_on")]
        public string SysUpdatedOn { get; set; }

        [JsonProperty("sys_tags")]
        public string SysTags { get; set; }

        [JsonProperty("sys_class_name")]
        public string SysClassName { get; set; }

        [JsonProperty("published_ref")]
        public string PublishedRef { get; set; }

        [JsonProperty("sys_id")]
        public string SysId { get; set; }

        [JsonProperty("sys_package")]
        public SysPackage SysPackage { get; set; }

        [JsonProperty("inactive")]
        public string Inactive { get; set; }

        [JsonProperty("sys_update_name")]
        public string SysUpdateName { get; set; }

        [JsonProperty("sys_updated_by")]
        public string SysUpdatedBy { get; set; }

        [JsonProperty("sys_created_on")]
        public string SysCreatedOn { get; set; }

        [JsonProperty("sys_name")]
        public string SysName { get; set; }

        [JsonProperty("sys_scope")]
        public SysScope SysScope { get; set; }

        [JsonProperty("text")]
        public string Text { get; set; }

        [JsonProperty("value")]
        public string Value { get; set; }

        [JsonProperty("sys_created_by")]
        public string SysCreatedBy { get; set; }

        [JsonProperty("misc")]
        public string Misc { get; set; }

        [JsonProperty("order")]
        public string Order { get; set; }

        [JsonProperty("sys_policy")]
        public string SysPolicy { get; set; }
    }

    public class Wrapper
    {

        [JsonProperty("result")]
        public IList<Result> Result { get; set; }
    }

}

Showing that items comes up empty

JSON format I had to edit some of the values and obv its way longer but this should give an idea

{
    "result": [
        {
            "rec_misc": "0",
            "question": {
                "link": "link goes here",
                "value": "494a2"
            },
            "sys_mod_count": "1",
            "sys_updated_on": "2021-11-15 17:18:10",
            "sys_tags": "",
            "sys_class_name": "question_choice",
            "published_ref": "",
            "sys_id": "",
            "sys_package": {
                "link": "link goes here",
                "value": "global"
            },
            "inactive": "false",
            "sys_update_name": "question_choice_",
            "sys_updated_by": "someone ",
            "sys_created_on": "2021-11-15 17:17:35",
            "sys_name": "BG Info",
            "sys_scope": {
                "link": "link goes here",
                "value": "global"
            },
            "text": "BG Info",
            "value": "BG Info",
            "sys_created_by": "someone",
            "misc": "0",
            "order": "300",
            "sys_policy": ""
        },
        {
            "rec_misc": "0",
            "question": {
                "link": "link goes here",
                "value": "5b42"
            },
            "sys_mod_count": "0",
            "sys_updated_on": "2021-04-14 06:56:39",
            "sys_tags": "",
            "sys_class_name": "question_choice",
            "published_ref": "",
            "sys_id": "05c2d",
            "sys_package": {
                "link": "link goes here",
                "value": "global"
            },
            "inactive": "false",
            "sys_update_name": "question_choice_",
            "sys_updated_by": "someone",
            "sys_created_on": "2021-04-14 06:56:39",
            "sys_name": "Baseline (Developer Plus Build Mgr)",
            "sys_scope": {
                "link": "link goes here",
                "value": "global"
            },
            "text": "Baseline (Developer Plus Build Mgr)",
            "value": "baseline",
            "sys_created_by": "someone",
            "misc": "0",
            "order": "300",
            "sys_policy": ""
        },

CodePudding user response:

Use

Wrapper results = JsonConvert.DeserializeObject<Wrapper>(result);

The response is coming as an array so you have to map the data properly.

  • Related