how to pass null value in the json while constructing it
I have the below code where I am assigning values coming from a string to a key value in here, for ex below I am assigning obj[_id] to id.But there is possibility that my jstr do not contain _id key and in that case I would like to assign value "null" to my id. How can I do that in below code?
foreach (JObject obj in JArray.Parse(jStr))
{
var options = new JsonTranslatorOptions(
kind: "ENTITY",
id: obj["_id"],
type: "EQUIPMENT",
version: "data-4-build-606");
var translator = new JsonTranslator(options);
output = translator.Translate(obj);
}
internal class JsonTranslatorOptions
{
public JsonTranslatorOptions(
string kind,
JToken id,
string type,
string version)
{
Kind = kind;
Id = id.ToString();
Type = type;
Version = version;
}
public string Kind { get; }
public string Id { get; }
public string Type { get; }
public string Version { get; }
}
Are you accessing id.ToString()
when id
is null
?
try this:
public JsonTranslatorOptions(
string kind,
JToken id,
string type,
string version)
{
Kind = kind;
Id = id?.ToString(); // null check
Type = type;
Version = version;
}