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 7760779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:58:48+00:00 2026-06-01T13:58:48+00:00

I’m checking if an object in a JSON string exists using this: JSONObject json

  • 0

I’m checking if an object in a JSON string exists using this:

JSONObject json = null;

         try {
            json = new JSONObject(myJsonString);
        } catch (JSONException e) { e.printStackTrace(); } 


        if(json.has("myObject")) System.out.println("EXISTS");

        else System.out.println("DOESN'T EXIST");

The problem appears when I attempt to check if a sub object exists. e.g:

...,"queue":{"building":{"q0":{"id":177779,...

Queue always exists and building also, but q0 is not always there. So, how can I check the existence of q0? And, is there a way to check it using the Gson library?

Thank you in advance!

  • 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-01T13:58:50+00:00Added an answer on June 1, 2026 at 1:58 pm

    You can simply give it a try and return null if the try failed. Or you can break your attempt up into little pieces to monitor where it fails.

    /**
     * This method will return the JSONObject q0, if it exists
     * If it doesn't exist it will return NULL
     *
     */
    private JSONObject getQZero(JSONObject json)
    {   
        try
        {
            return json.getJSONObject("queue").getJSONObject("building").getJSONObject("q0");
        }
        catch (JSONException e)
        {
            // This could be triggered either because there is no q0
            //   or because the JSON structure is different from what was expected.
            return null;
        }    
    }
    

    You could also go step by step, if you want to print logs for each level;

    /**
     * This method will show where your jsonparsing fails.
     * It will throw a JSONOException if the json is way different from what 
     *   was expected, and otherwise it will print a log of where the parsing
     *   failed.
     */
    private JSONObject getQZero(JSONObject json) throws JSONException
    {       
        // Stop if no queue
        if (! myObject.has("queue") 
        {
            Log.d(TAG, "no queue!");
            return null;
        }
    
        JSONObject queue = myObject.getJSONObject("queue");
    
        // Stop if no building
        if (! queue.has("building")
        {
            Log.d(TAG, "no building!");
            return null;
        }
    
        JSONObject building = queue.getJSONObject("building")
    
        // Stop if no q0
        if (! building.has("q0"))
        {
            Log.d(TAG, "no q0!");
            return null;
        }
    
        JSONObject q0 = building.getJSONObject("q0");
        // Q0 is returned here. If the method returned earlier, it returned NULL
        // You could also do nested ifs, but the indentation gets crazy
        return q0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

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.