Jeremy Winslow Jeremy Winslow - 2 months ago 7
JSON Question

Loop JSON objects may not always exist

I have a complex JSON object I'm having difficulty with. The problem is that sometimes, a sub list of items within the JSON model may not exist for each element. Ie. completely missing.

The JSON objects vary, for the few elements returning everything, I can often get 2 or 3 complete returned items so I know my object model for at least some part, works but for example Name1 from the below model will return successfully. But then errors parsing Name2. So I can't seem to loop through the entire list without an Exception.

"Object reference not set to an instance of an object"


I tried to wrap the outputs within Try Catch blocks however I am still returning an error.

Sample JSON with all use cases

[
{
"name": "Name1",
"description": "",
"location": "ANY",
"inputs": [
{
"name": "input1",
"required": true,
"description": "some short description"
},
{
"name": "input2",
"required": true,
"description": "another short description"
}
],
"outputs": [
{
"name": "output1",
"required": false
},
{
"name": "outpu2",
"required": false
}
]
},
{
"name": "Name2",
"description": "some long description",
"location": "ANY",
"inputs": [ {
"name": "input1",
"required": false,
"description" : "some short description of the input"
}]
},
{
"name": "Name3",
"description": "",
"location": "ANY"
}
]


My C# Definitions work for the first reference for this JSON object, however for "Name2 and "Name3" I am getting this "object reference not set to an instance of an object" error.

My C# Json Definitions

public class inputParameters
{
[JsonProperty("name")]
public string inputName { get; set; }

[JsonProperty("required")]
public bool inputRequired {get; set;}

[JsonProperty("description")]
public string inputDescription {get; set;}
}
public class outputParameters
{
public string outputName { get; set; }
public bool outputRequired { get; set; }
}
public class JsonObject
{
[JsonProperty("name")]
public string ProcessName { get; set; }

[JsonProperty("description")]
public string ProcessDescription { get; set; }

[JsonProperty("peerLocation")]
public string PeerLocation { get; set; }

public List<inputParameters> InputParameters { get; set; }
public List<outputParameters> OutputParameters{ get; set; }
}


And my Deserializing object and loops

var Object = JsonConvert.DeserializeObject <List<JsonObject>>(txt);

foreach (JsonObject JsonObject in Object)
{
Console.WriteLine("Process Name: " + JsonObject.ProcessName);
Console.WriteLine("PeerLoacatoin: " + JsonObject.PeerLocation);
Console.WriteLine("Process Description: " +JsonObject.ProcessDescription);


foreach (var InputParams in JsonObject.InputParameters)
{
try
{
Console.WriteLine("Input Name: " + InputParams.inputName);
Console.WriteLine("Input Required: " + InputParams.inputRequired);
Console.WriteLine("Input Description: " + InputParams.inputDescription);
}

catch(Exception)
{
InputParams.inputName = "";
InputParams.inputRequired = false;
InputParams.inputDescription = "";
}

}
foreach (var OutputParams in JsonObject.OutputParameters)
{
try
{
Console.WriteLine("Output Name: " + OutputParams.outputName);
Console.WriteLine("Output Required: " + OutputParams.outputRequired);
}
catch (Exception)
{
OutputParams.outputName = "";
OutputParams.outputRequired = false;
}
}
Console.WriteLine();

Answer

I would create a constructor which initializes all your Lists to new List. That way you can safely iterate without getting null pointer exceptions.

class JsonObject
{
    public JsonObject()
    {
        InputParameters = new List<inputParameters>();
        OutputParameters = new List<outputParameters>();
    }
}