4

I have a json object like so:

{
    "Name": "Mike",
    "Personaldetails": [
        {
            "Age": 25,
            "Surname": "Barnes"
        }
    ],
    "Address": [
        null
    ]
}

Now I have written C# to access this code and iterate over each object in the "Personal Details" array and into "Address" array.

How would I write a check to see if the array is null?

dynamic jsonObject = JsonConvert.DeserializeObject(data);

foreach (var obj in jsonObject.Personaldetails)
{
     if (obj.Age = 24)
     {
          //do stuff
     }
}

//This is where I am stuck
if(jsonObject.Address = null)
{
    return "null array";
}
//If another json stream was not null at "Address" array
else
{
    foreach (var obj in jsonObject.Address)
    {
         if (obj.arrayItem == "Something")
         {
              //do stuff
         }
    }
}
6
  • 1
    you need to use == instead of = for comparisons in the if statements Commented Aug 15, 2013 at 13:55
  • 1
    What you've posted is not valid JSON (specifically the Personaldetails property's value). Commented Aug 15, 2013 at 13:57
  • happy now @Jonesy and @Andrew? Commented Aug 15, 2013 at 14:02
  • We're trying to help, take it easy. Also your code doesn't reflect those changes Commented Aug 15, 2013 at 14:07
  • Thanks for the updates--this still won't run for me though. I get a runtime error that Newtonsoft.Json.Linq.JObject does not contain a definition for Personaldetails. Are you using JSON.NET? Commented Aug 15, 2013 at 14:10

2 Answers 2

7

Right as no one else seems to be paying attention, here is the answer...

The problem is with this bit of code:

"Address": [
    null
]

You are trying to check if Address is null, however this JSON does not represent a null Address. It shows a valid array, with one single null object. If this is correct, then you may want to try this:

if(jsonObject.Address == null || (jsonObject.Address[0] == null))
{
    return "null array";
}

Firstly, notice the use of == to check equality (rather than = for assignment).

Secondly, this will check if Address is null OR if the first object of the array is null, which I assume is what you are trying to do. It may also be worth adding in a length check to see if the array is just a single null element - but that depends on your requirements

Sign up to request clarification or add additional context in comments.

3 Comments

Wow @musefan you're really good a reiterating previously posted answers.
@RobCarroll: Wrong, your answer mentioned nothing about checking the first item in the array at the time I started to write my answer. When I started writing, your answer only made comment about the = > == solution, which is only part of the problem
@RobCarroll First, the operator should be || not &&. Second your Count() will throw exception. I guess you posted it without checking....
1

Your missing the double "==" to check for comparison, "=" is an assignment operation.

if(jsonObject.Address == null)
{
    return "null array";
}

Your JSON should look like this. Otherwise your not checking for a null array but instead an array with a null value for the first element of the array.

{
    "Name": "Mike",
    "Personaldetails": [
        {
            "Age": 25,
            "Surname": "Barnes"
        }
    ],
    "Address":  null
}

Then the code would be the following:

if(jsonObject.Address == null)
{
    return "null array";
}

If you need to keep the original JSON you can always do a check like so:

if(jsonObject.Address.Length > 0 && jsonObject.Address[0] == null)
{
    return "null array";
}

5 Comments

Yeah but the problem is the array is not null. It is an array that contains a single null value
Your JSON is not valid then, use jsonviewer.stack.hu or jsonmate.com to validate your JSON. Is this JSON that you have created or JSON that is being passed to you?
No I did not get this passed to me @musefan, I quickly created it and forgot to use {} for items within a Json array. So please guys its just a simple mistake which I have corrected and validated.
If you look at the time of my question edit you will see that I have corrected the json sample.
.count() ?? a) starts with lowercase b) Linq with dynamic ?

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.