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

  • SEARCH
  • Home
  • 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 7085319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:24:36+00:00 2026-05-28T07:24:36+00:00

In Java, if I use the following JSON string as an example, how would

  • 0

In Java, if I use the following JSON string as an example, how would I check if the objects are empty/null?

{"null_object_1" : [], "null_object_2" : [null] }

I have tried using:

if(!jsonSource.isNull("null_object_1"))  {/*null_object_1 is not empty/null*/}
if(!jsonSource.isNull("null_object_2"))  {/*null_object_2 is not empty/null*/}

But these IF statements still return true (as if they are not empty/null).

Does anyone have a solution?

Edit:
By “object”, I actually meant array.

  • 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-05-28T07:24:37+00:00Added an answer on May 28, 2026 at 7:24 am

    Neither of those two things are null; they are arrays. The result is exactly what you would expect.

    One of your arrays is empty, the other contains a single element that is null.

    If you want to know if an array is empty, you would need to get the array, then check its length.

    JSONObject myJsonObject = 
        new JSONObject("{\"null_object_1\":[],\"null_object_2\":[null]}");
    
    if (myJsonObject.getJSONArray("null_object_1").length() == 0) {
        ... 
    }
    

    Edit to clarify: An array having no elements (empty) and an array that has an element that is null are completely different things. In the case of your second array it is neither null nor empty. As I mentioned it is an array that has a single element which is null. If you are interesting in determining if that is the case, you would need to get the array then iterate through it testing each element to see if it were null and acting upon the results accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following example json output form java/jsonsimple. The JSONArray is: [{dd1:{actionType:Dept,collegeID:}},{dd2:{actionType:Dept,collegeID:}}] I'm
I'm converting an application to use Java 1.5 and have found the following method:
Why one would use one of the following packages instead of the other? Java
I'm working with java, trying to use the AudioFormat Class. I have the following
I have the following Json: [{label:75001,value:75001}, {label:75002,value:75002}, {label:75003,value:75003}, {label:75004,value:75004}, {label:75005,value:75005}, {label:75006,value:75006}, {label:75007,value:75007}, {label:75008,value:75008}, {label:75009,value:75009}]
I would like to use Atmosphere in one of my projects and have some
I have a Servlet that sends back a JSON Object and I would like
I'm an aspiring Java programmer looking to use JSON in a project. I was
I am trying to put String[] in jsonObject and getting following error java.lang.IllegalArgumentException: Invalid
I have a spring MVC java application and I'm serializing joda DateTime to json.

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.