1

So I have a JSON file that has an object nested within it. It looks like this:

{ "MyObject" : { "prop1": "Value1", "prop2" : "Value2"}
  "Message" : "A message"}

Sometimes, the object nested inside the JSON may be null. So for example,

{ "MyObject" : null
  "Message" : "A message"}

I'm trying to deserialize this object using Newtonsoft.Json into a type T of JsonObject which has two properties:

  • MyObject and
  • Message.

JsonObject looks like this:

public class JsonObject
{
   public MyObject MyObject { get; set; }
   public string Message { get; set; }
}

Message is just a string, but MyObject is an object which has a few other (private) properties as well as a constructor:

public class MyObject
{
    public MyObject(string prop1, string prop2)
    {
        this.prop1= prop1;
        this.prop2= prop2;
    }

    public string prop1 { get; private set; }
    public string prop2 { get; private set; }
}

My code to deserialize looks like this:

using (StreamReader reader = new StreamReader(filename))
{
    string jsonFile = reader.ReadToEnd();
    return JsonConvert.DeserializeObject<JsonObject>(jsonFile);
}

But it is throwing an error:

Exception: Object reference not set to an instance of an object

This happens when the MyObject object is null. I printed jsonFile, and this is the output:

{"MyObject": null, "Message": "sample message"}

What I want is for MyObject to deserialize to null. So when the code runs, the end result is a JsonObject with

  • MyObject = null and
  • Message = "sample message".

How do I do this using Newtonsoft.Json?

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Tino
  • 304
  • 4
  • 14
  • 2
    Your JSON is malformed -- you need a comma between the two root object properties. I.e. `{ "MyObject" : { "prop1": "Value1", "prop2" : "Value2"}, "Message" : "A message"}` and `{ "MyObject" : null, "Message" : "A message"}`. Having fixed that, I can deserialize both JSON strings successfully. Can you make a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your problem? – dbc Mar 25 '15 at 22:56
  • If you want to use `System.Text.Json` this [similar question](https://stackoverflow.com/questions/74856371/) may be of use. – surfmuggle Dec 19 '22 at 22:42

1 Answers1

3

I created a small test with what you've provided above and found that MyObject and Message were missing a comma between them. Otherwise everything worked fine. Checking the objects via debug shows everything parses as it should.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            const string json1 = "{ \"MyObject\" : { \"prop1\": \"Value1\", \"prop2\" : \"Value2\"}, \"Message\" : \"A message\"}";
            const string json2 = "{ \"MyObject\" : null, \"Message\" : \"A message\"}";

            var obj1 = JsonConvert.DeserializeObject<JsonObject>(json1);
            var obj2 = JsonConvert.DeserializeObject<JsonObject>(json2);

            Assert.IsInstanceOfType(obj1, typeof(JsonObject));
            Assert.IsInstanceOfType(obj2, typeof(JsonObject));
        }
    }


    public class MyObject
    {
        public MyObject(string prop1, string prop2)
        {
            this.prop1 = prop1;
            this.prop2 = prop2;
        }

        public string prop1 { get; private set; }

        public string prop2 { get; private set; }
    }

    public class JsonObject
    {
        public MyObject MyObject { get; set; }
        public string Message { get; set; }
    }
}
Chris
  • 2,766
  • 1
  • 29
  • 34