Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8406687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:10:43+00:00 2026-06-09T23:10:43+00:00

For example, I write code like this bool hasCustomer = true; JObject j =

  • 0

For example, I write code like this

        bool hasCustomer = true;

        JObject j = JObject.FromObject(new
        {
            customer = hasCustomer? new
            {
                name = "mike",
                age = 48
            }:null
        });


        JObject c = (JObject)j["customer"];
        if (c != null)
            string name = (string) c["name"];

This works fine.

But if I set hasCustomer = false;

       JObject c = (JObject)j["customer"];

will throw an System.InValidCastException:

       Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'.

I was expecting should just assign null to JObject c since JObject is nullable.

So, what’s the best way to handle something like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:10:44+00:00Added an answer on June 9, 2026 at 11:10 pm

    Ignoring null, seems to be producing the correct behavior.

        bool hasCustomer = false ;
    
        JsonSerializer s = new JsonSerializer() {
            NullValueHandling = NullValueHandling.Ignore
        };
        JObject j = JObject.FromObject( new {
            customer = hasCustomer ? new {
                name = "mike" ,
                age = 48
            } : null
        }, s );
    
    
        JObject c = (JObject)j[ "customer" ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, is it possible to write code like this: int $x = 6;
Often I write classes like this: Logger::Logger(bool log_time_, bool log_percentage, bool log_size):log_time(log_time_)... //made up
I know this is bad practice. Don't write code like this if at all
I can write the code like this: str match { case s if s.startsWith(!!!)
I've heard that it is possible to write some code like this SomeClass obj
I frequently write throwaway code (in a research environment ) - for example to
So, I need to write an example code using the decorator dessign pattern and
My brother wrote this code for me to show an example of polymorhism. But
I tried to implement this example to write and read data from internal storage,
I'd like to minimize synchronization and write lock-free code when possible in a project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.