0

I want to pass a null value to a key using a POST request in an API.

For example, I want to pass the below JSON data. That is, Exp and TeamID is null.

{
   "ID":162617,
   "TextKey":"107737",
   "Exp":null,
   "TeamID":null
}

The result is accepted in Postman, but when I tried passing the same using C# code below, my JSON content becomes invalid.

long idvalue = 162617;
string textkeyvalue = "107737";
string expvalue = null;
long? teamIDvalue = null;

string postData = "{\"ID\":" + idvalue + ",\"TextKey\":\"" + textkeyvalue + "\",\"Exp\":\"" + expvalue + "\",\"TeamID\":\"" + teamIDvalue + "\"}";

Which gives me the following output.

{
   "ID":162617,
   "TextKey":"107737",
   "Exp":"",
   "TeamID":
}

And my request fails due to the invalid JSON body. So how do i pass this sort of null data or null keyword?

Note : All the Key value pairs are mandatory in my API, so I cannot omit them if they are null.

I just want to pass the data in the below format.

{
   "ID":162617,
   "TextKey":"107737",
   "Exp":null,
   "TeamID":null
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user8883996
  • 133
  • 2
  • 7
  • Don't quite get the problem. If its a null then explicitly write null without quotes. Besides that you should think about creating a "real" class for the data and use a standard jsonserializer. It will deal with the null. – Ralf Mar 31 '23 at 12:07
  • actually I am getting those null values from some other api which i feeding into this new api i'm working with. – user8883996 Mar 31 '23 at 12:10
  • 2
    First of all, you shouldn't create a JSON string by hand, but use some library ... What if for instance `textKey` contains a quote `"`? – derpirscher Mar 31 '23 at 12:22
  • Does this answer your question? [How to create JSON string in C#](https://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c-sharp) – Charlieface Mar 31 '23 at 14:20

2 Answers2

3

To answer your question, to write text for a null value, you can do this:

var result = "my value is " + (strValue ?? "null");

Or to add quotes:

var result = "my value is " + (strValue == null ? "null" : $"\"{strValue}\"");

You can also create static helper methods to make it easier:

static string write(string str) => str == null ? "null" : $"\"{str}\"";
static string write(long? value) => value == null ? "null" : value.ToString();

So in your example, it becomes:

string postData = "{\"ID\":" + idvalue + ",\"TextKey\":" + write(textkeyvalue) +
                  ",\"Exp\":" + write(expvalue) + ",\"TeamID\":" +
                  write(teamIDvalue) + "}";

Better solution!

Create a class for the data model, and use a library (for example, System.Text.Json) to serialize it, like so:

public class MyData
{
   public long ID { get; set; }
   public string TextKey { get; set; }
   public string Exp { get; set; }
   public long? TeamID { get; set; }
}

// Construct model
var data = new MyData()
{
   ID = 162617,
   TextKey = "107737",
   Exp = null,
   TeamID = null,
}

// Serialize to JSON
var result = System.Text.Json.JsonSerializer.Serialize(data);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gepa
  • 404
  • 2
  • 8
  • Thank you so much for explanation @gepa this worked. Sorry I forgot to mention that I was running .net 4.0 so System.Text.Json did not work for me. so i used [DataContractJsonSerializer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.json.datacontractjsonserializer?redirectedfrom=MSDN&view=net-7.0) using your structure and it worked perferctly. thank you so much – user8883996 Apr 01 '23 at 12:24
3

gepa’s answer is right on the money, but you can serialize an anonymous object, too:

long idvalue = 162617;
string textkeyvalue = "107737";
string? expvalue = null;
long? teamIDvalue = null;

string postData = System.Text.Json.JsonSerializer.Serialize(new
{
    ID = idvalue,
    TextKey = textkeyvalue,
    Exp = expvalue,
    TeamID = teamIDvalue,
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
padeso
  • 390
  • 1
  • 8
  • Thank you @padeso this worked. Sorry I forgot to mention that I was running .net 4.0 so System.Text.Json.JsonSerializer did not work for me. so i used [DataContractJsonSerializer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.json.datacontractjsonserializer?redirectedfrom=MSDN&view=net-7.0) using your structure and it worked perfectly. thank you so much. – user8883996 Apr 01 '23 at 12:26