JavaScript Select parts of JSON and check if value is empty

I have a question about selecting parts of a JSON object and check if they have a value or not.
I load the JSON from an API in PHP:

$json_url  ='http://api.url.com/api/gateway/call/1.4/getApp?appid=2631';
$ch = curl_init($json_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$str = curl_exec($ch);
curl_close($ch);
$data = json_decode($str);

output of data is:

object(stdClass)[1]
      public 'app' => 
        array
          0 => 
            object(stdClass)[2]
              public 'id' => string '2631' (length=4)
              public 'apptypeid' => string '3' (length=1)
              public 'organizerid' => string '913' (length=3)
              public 'accountId' => string '687' (length=3)
              public 'name' => string 'Meet in Liege' (length=13)
              public 'info' => string '' (length=0)
              public 'submit_description' => string '' (length=0)
              public 'category' => string '' (length=0)
              public 'app_icon' => string 'upload/appimages/2631/icon1024.png' (length=34)
              public 'homescreen' => string '' (length=0)
              public 'token' => string 'e6615c94a8b0fed28cdbec611669b19b' (length=32)
              public 'certificate' => string 'app2631' (length=7)
              public 'subdomain' => string 'mil13' (length=5)
              public 'cname' => string '' (length=0)
              public 'advancedFilter' => string '0' (length=1)
              public 'status' => string 'inactive' (length=8)
              public 'isdeleted' => string '0' (length=1)
              public 'creation' => string '2013-03-19 14:20:44' (length=19)
              public 'defaultlanguage' => string 'fr' (length=2)
              public 'visibleintapcrowd' => string '0' (length=1)
              public 'applestoreid' => string '0' (length=1)
              public 'address' => string '' (length=0)
              public 'lat' => string '0' (length=1)
              public 'lon' => string '0' (length=1)
              public 'telephone' => string '' (length=0)
              public 'email' => string '' (length=0)
              public 'website' => string '' (length=0)
              public 'contentmodule_css_phone' => string '' (length=0)
              public 'contentmodule_css_tablet' => string '' (length=0)
              public 'searchterms' => string '' (length=0)
              public 'fbid' => string '563672167017349' (length=15)
              public 'dropdb' => string '0000-00-00 00:00:00' (length=19)
              public 'bundle' => string 'com.tapcrowd.meetinliege2631' (length=28)
              public 'blankurl' => string '' (length=0)
              public 'header_html_phones' => string '' (length=0)
              public 'header_html_tablets' => string '' (length=0)
              public 'footer_html_phones' => string '' (length=0)
              public 'footer_html_tablets' => string '' (length=0)
              public 'themeid' => string '41' (length=2)
              public 'timestamp' => string '1368548676' (length=10)
              public 'subflavorid' => string '12' (length=2)
              public 'subflavortype' => string '3' (length=1)
              public 'subflavorpaidtype' => string '9999' (length=4)
              public 'show_external_ids_in_cms' => string '0' (length=1)
              public 'launcherview' => string '' (length=0)
              public 'responsive' => string '1' (length=1)
              public 'apptype' => string 'Event Flavor' (length=12)
              public 'channel' => string '1' (length=1)
              public 'aboutbuttonkey' => string 'About TapCrowd' (length=14)
              public 'aboutbuttonurl' => string 'http://m.tap.cr/about' (length=21)
      public 'appversion' => string '' (length=0)
      public 'events' => 
        array
          0 => 
            object(stdClass)[3]
              public 'id' => string '5004' (length=4)
              .......

Now I would like to check if for example 'info', 'app_icon', 'address' and 'email' of app array are empty or not.
Does anyone know how I can do this? And is this possible with JSON queries? And I would like to do my operations in javascript!

Answer:1

Have you tried this:

if(empty($data->app[0]->info)){
    // info empty
}
if(empty($data->app[0]->app_icon)){
    // app_icon empty
}
if(empty($data->app[0]->address)){
    // address empty
}
if(empty($data->app[0]->email)){
    // email empty
}

If you're passing the json to javascript then use (assuming the json is assigned to a variable called data):

if(data.app[0].info.length === 0){
    // info empty
}

Then repeat for the others by replacing info with the name you want to check.

Answer:2

json_decode returns a regular PHP object, so it should be easy. If I'm not mistaken, given your $data object's structure this would do it:

// Retrieve the app object from $data
$app = $data->app[0];

// Check whatever you want with the object elements
if empty($app->info) "info empty\n";
if empty($app->app_icon) "app_icon empty\n";
if empty($app->address) "address empty\n";
if empty($app->email) "email empty\n";

In case there are more app object entries in the array you might consider using foreach to process them all.

Answer:3

I'm trying to implement this on my site so the nav style changes as you scroll into corresponding sections. http://stanhub.com/sticky-header-change-navigation-active-class-on-page-scroll-with-jquery/ ...

I'm trying to implement this on my site so the nav style changes as you scroll into corresponding sections. http://stanhub.com/sticky-header-change-navigation-active-class-on-page-scroll-with-jquery/ ...

  1. react-sticky cannot read property 'getboundingclientrect' of undefined
  2. cannot read property 'getboundingclientrect' of null react sticky

I'm trying out a simple one page render and I'm obviously doing it wrong: var express = require('express'); var app = express(); app.get('/', function(req, res){ res.render('home.html'); }); app....

I'm trying out a simple one page render and I'm obviously doing it wrong: var express = require('express'); var app = express(); app.get('/', function(req, res){ res.render('home.html'); }); app....

  1. error when rendering 'memory.total physical'
  2. error when rendering root instance
  3. error when rendering the page
  4. error when rendering state with content
  5. error when rendering component
  6. internal error when rendering 3ds max
  7. error rendering shape google slides
  8. error rendering view
  9. error rendering shape
  10. error rendering premiere pro
  11. error rendering coverage on the fast path
  12. error rendering image google slides
  13. error rendering macro 'viewxls'
  14. error rendering view jsf
  15. error rendering macro 'toc'
  16. error rendering macro 'excerpt-include'
  17. error rendering macro 'jira'
  18. error rendering template for decorator root
  19. error rendering view /index.xhtml
  20. error rendering control in asp.net

Is there a way to order an array by the output of another filter? I have an expression like this: <li ng-repeat="sub in subscriptions"> {{sub | subscriptionOutput}} </li> Here ...

Is there a way to order an array by the output of another filter? I have an expression like this: <li ng-repeat="sub in subscriptions"> {{sub | subscriptionOutput}} </li> Here ...

  1. combining like terms
  2. combining like terms worksheet
  3. combining pdf files
  4. combining form
  5. combining sentences
  6. combining cells in excel
  7. combining pdfs
  8. combining synonym
  9. combining like terms worksheet pdf
  10. combining vowel
  11. combining functions
  12. combining names
  13. combining sentences worksheet
  14. combining like terms activity
  15. combining like terms and distributive property worksheet
  16. combining word documents
  17. combining like terms puzzle
  18. combining like terms with negative coefficients
  19. combining exponents
  20. combining like terms video

This is quite an in depth question so I will try my best to explain in as much detail as possible. I am creating a Wordpress game information website, there are rows of gamelistings which when ...

This is quite an in depth question so I will try my best to explain in as much detail as possible. I am creating a Wordpress game information website, there are rows of gamelistings which when ...