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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:58:12+00:00 2026-06-15T21:58:12+00:00

My problem is this: I have an end point that will return JSON containing

  • 0

My problem is this:

I have an end point that will return JSON containing a structure named: the_data_i_want

If the server detects there is a value for the_data_i_want then it returns something like this:

{
    "sample": {
        "title": "The Title",
        "foo": {
            "Bar": {
                "Baz": {
                    "Qux": {
                        "the_data_i_want": "DATA PARTY!!!!"
                    }
                }
            }
        }
    }
}

If the server detects that the_data_i_want is empty, the JSON it returns looks something like this:

{
    "sample": {
        "title": "The Title"
    }
}

My problem is that because any segment of the chain sample.foo.Bar.Baz.Qux.the_data_i_want my be undefined i end up doing these Crazy chains of checks on each step through the json structure where i look and first see if it’s undefined, then if it isn’t, i try the next one and next one until i get to the_data_i_want and it seems wrong.

The question:

Is there a “correct” way to handle this? What would i do in the even the nesting is arbitrarily deep? I’m open to using jQuery.

thank you!

  • 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-15T21:58:12+00:00Added an answer on June 15, 2026 at 9:58 pm

    May be this might work.. But there may be better methods out there..

    In this approach I have all the keys in an array and check them in a loop..

    var obj = {
        "sample": {
            "title": "The Title",
            "foo": {
                "Bar": {
                    "Baz": {
                        "Qux": {
                            "the_data_i_want": "DATA PARTY!!!!"
                        }
                    }
                }
            }
        }
    };
    
    var arr = ["foo", "Bar", "Baz", "Qux", "the_data_i_want"];
    var arr1 = ["foo", "Bar", "Baz", "Qux", "the_data_i_want", "ding"];
    
    checkKey('Object 1' , arr);
    checkKey('Object 2 ' ,arr1);
    
    function checkKey(str ,arr) {
        if (obj && obj.sample) {
            var k = obj.sample;
            var i = 0;
            for (var i = 0; i < arr.length; i++) {
                if (k[arr[i]]) {
                    k = k[arr[i]];
                    if (i === arr.length - 1) {
                        alert('Data Found :: ' + k);
                    }
                }
                else {
                    alert('No such Key in :: ' + str );
                    break;
                }
            }
        }
        else {
             alert('No such Key in :: ' + str );
        }
    }​
    

    Check Fiddle

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

Sidebar

Related Questions

In the end, I have decided that this isn't a problem that I particularly
I am sure that this problem will be easily resolved however I am relatively
I have this pattern written ^.*\.(?!jpg$|png$).+$ However there is a problem - this pattern
I'm developing an Android application with a spinner. I have this problem: This is
I have this problem: I want to generate a new source code file from
I have this problem: $id is id for each user $img = 'http://www.somesite.com/pictures/'; $no_img
I have this problem since I'm beginning in OOP programming I want to close
I have this problem when I run SAS 9.2 on the command line on
I have this problem, when I load my form, the validation messages appears in
I have this problem in my SlashDot Menu. I want to change the picture

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.