Json Serialize Ignore Property

Ali Yılmaz 81 Reputation points
2023-04-14T06:04:45.13+00:00

Hi, I want to delete a property in my model. When I serialize json, I want json result not to appear. how is this possible. Class


    public class SkuVariant
    {

        public int fldVariant1TypeId { get; set; }

        public string fldVariant1TypeName { get; set; }

        public int fldVariant2TypeId { get; set; }
        public string fldVariant2TypeName { get; set; }

        public int fldVariant3TypeId { get; set; }
        public string fldVariant3TypeName { get; set; }

        [JsonIgnore(Condition = JsonIgnoreCondition.Always)]
        public string? Sku { get; set; }
    
    }

Dapper Query


 Select AL.[Articel.Code] AS Sku , 168 AS fldVariant1TypeId, BD.MainColor_Turkish AS fldVariant1TypeName, 
				169 AS fldVariant2TypeId , BS.Value AS fldVariant2TypeName ,
				169 AS fldVariant3TypeId, BSC.Value AS fldVariant3TypeName  
                 from vw_ArticelHorVerValueInfo AL

Controller Json


  var jsons = JsonConvert.SerializeObject(first);
Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xinran Shen - MSFT 2,091 Reputation points
    2023-04-14T07:54:58.1666667+00:00

    Hi @Ali Yılmaz, If you want to ignore specified property when serializing, You can use [JsonIgnore] under System.Text.Json.Serialization namespace. Please refer to this simple demo:

    using System.Text.Json.Serialization;
    
    namespace WebApplication1.Model
    {
        public class TestModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
            [JsonIgnore]
            public string Country { get; set; }
        }
    }
    

    Controller

    
     [HttpGet]
    
        public IActionResult Get()
        {
    
            List<TestModel> models = new List<TestModel>();
            TestModel model = new TestModel()
            {
                Id = 1,
                Name = "aaaa",
                Country = "USA"
            };
    
            TestModel model2 = new TestModel()
            {
                Id = 2,
                Name = "bbb",
                Country = "CN"
            };
    
            models.Add(model);
            models.Add(model2);
            var json = JsonSerializer.Serialize(models);
            return Ok(json);
        }
    

    Result enter image description here

    You can find it ignore Country property successfully.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Xinran Shen


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.