Home
Q&A
Education
Technology
Credit
General
Health
Insurance
Questions
Ask a Question
ignore null values globally in JSON output in aspnet method
Home
Education
ignore null values globally in JSON output in...
asked
Apr 7, 2022
in
Education
by
JackTerrance
newbie question: how do i make my JSON output ignore null values? I don't want to necessarily set each individual property to ignore null (as in decorate each property with [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] ), and few different global methods I found and tried didn't work. I am using .Net 6 and Newtonsoft.Json I have this method in my controller [HttpPost] public async Task Post([FromBody] RequestJson value) { DataProcessor processor = new DataProcessor(value); return processor.GetResults(); } This is what ResponseJson looks like (with some properties omitted for brevity). public class ResponseJson { [JsonProperty(PropertyName = "items")] public List Items { get; set; } } public class Item { [JsonProperty(PropertyName = "name")] public string name { get; set; } [JsonProperty(PropertyName = "colour")] public string colour { get; set; } [JsonProperty(PropertyName = "parameters")] public ItemParameters parameters { get; set; } } DataProcessor doesn't set the colour (null), or doesn't set ItemParameters at all for some of the Item. When looking at the response from calling this method, the JSON string looks like this: { "items": [ { "name":"abc", "colour": "blue", "parameters":{
} }, { "name":"def", "colour": null "parameters":null }, { "name":"ghi", "colour": null, "parameters":null }, { "name":"jkl", "colour": "red", "parameters":{
} } ] } I want the properties with null values to be ignored completely so that it looks like this: { "items": [ { "name":"abc", "colour": "blue", "parameters":{
} }, { "name":"def" }, { "name":"ghi" }, { "name":"jkl", "colour": "red", "parameters":{
} } ] } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)
c#asp
netnulljson
net
1
Answer
0
votes
answered
Apr 7, 2022
by
JackTerrance
did you try this way? services.AddControllersWithViews().AddNewtonsoftJson(o => { o.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; });
Related questions
0
votes
Q: ignore null values globally in JSON output in aspnet method
newbie question: how do i make my JSON output ignore null values? I don't want to necessarily set ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Apr 7, 2022
in
Education
by
JackTerrance
c#asp
netnulljson
net
0
votes
Q: Asp .NET MVC Hide Row and update DB values
I have a database (SQL Server) with clients and a MVC plataform, and i want a row to be "hidden ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
May 7, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: Asp .NET MVC Hide Row and update DB values
I have a database (SQL Server) with clients and a MVC plataform, and i want a row to be "hidden ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
May 4, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: ASP.NET I don't get a value from textbox field on GridView RowUpdating event
So the problem is i don't get get a value from textbox field on GridView RowUpdating event. Here ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Jul 8, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: Dynamically created Dropdownlist inside Gridview doesnot fire event on second time
I have been developing a webpage which uses n number of dropdownlists which are binding dynamically inside a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Jun 18, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: "The DefaultButton of 'Panel1' must be the ID of a control of type IButtonControl." error in asp.net
I have a Login control placed inside LoginView control contained within a PANEL. I want to set login button ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Jun 8, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: Download file with ClosedXML
All How can I download a file so the user sees that it is downloading (like with a stream?) I ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Jun 2, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: How to access asp.net controls from DNN skin code behind
I'm working on a DNN skin using Christoc's DNN templates and when I create a normal ASP.NET ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
May 26, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: Asp ToolkitScriptManager not know element in visual studio 2015
I have tried many solution online but I am unable to fix. First off all i check in bin and it ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
May 7, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: User-defined conversion must convert to or from the enclosing type in asp core 2.2
i create this class and it have this constructor : public class ReturnResult : ImplicitReturnResult { public bool ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Apr 24, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: An invalid request URI was provided. (No helpful info in the stack trace)
Pulling my hair out here. All was working fine, and then out of the blue, I started to get some ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Apr 21, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: How do I read an HttpResponse in ASP.NET 2.0?
For example, I have an ASP.NET form that is called by another aspx: string url = "http:// ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Mar 21, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: Accessing SMTP Mail Settings from Web.Config File by using c#
Need to read my SMTP email settings defined under system.net section in my web.config file. Below is one example of SMTP email setting defined in web.config file: (Under Section)...
asked
Mar 2, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: Return category hierarchy in a string like "cat1 > cat2 > cat3"
I have an SQL table in the following format: id categoryname parent id 1 cat1 0 2 cat2 1 3 cat3 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Feb 27, 2022
in
Education
by
JackTerrance
c#asp
net
0
votes
Q: Return category hierarchy in a string like "cat1 > cat2 > cat3"
I have an SQL table in the following format: id categoryname parent id 1 cat1 0 2 cat2 1 3 cat3 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Feb 27, 2022
in
Education
by
JackTerrance
c#asp
net
...