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?

2
  • 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 of your problem? Commented Mar 25, 2015 at 22:56
  • If you want to use System.Text.Json this similar question may be of use. Commented Dec 19, 2022 at 22:42

1 Answer 1

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; }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.