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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:49:26+00:00 2026-05-21T12:49:26+00:00

I’m new to this so please bear with me. I’m dealing with the Twitter

  • 0

I’m new to this so please bear with me. I’m dealing with the Twitter API which relies heavily on JSON (and that’s not a bad thing).

Something that Twitter does is return some values as comma seperated objects. This seems to be isolated to the [entities] section, which enumerates links within a tweet.

This is a function I’m using to reduce json to unordered lists. The section commented out of this code is what I need help with.

function buildULfromJSON(data){
  var items = [];

  function recurse(json){                               //declare recursion function
    items.push('<ul>');                                 // start a new <ul>

    $.each(json, function(key, val) {                   // iterate through json and test, treat vals accordingly

      if((val == '[object Object]') && (typeof val == 'object')){ // this catches nested json lists
        items.push('<li>[' + key + '] =></li>');                  // adds '[key] =>'
        recurse(val);                                             // calls recurse() to add a nested <ul>

      }else if(typeof(val)=='string'){                            // catch string values
        items.push('<li>[' + key + '] = \"' + val + '\"</li>');   // add double quotes to <li> item

      }/* else if(typeof(val) == 'object'){                      // this throws exception in jquery.js
        $.each(val, function(){                                  // when val contains [object Object],[object Object]
          items.push('<li>[' + key + '] =></li>');               // need to test for and process 
          recurse(val);                                          // comma seperated objects
        });
      } */else{
        items.push('<li>[' + key + '] = ' + val + '</li>');     // non string, non object data (null, true, 123, .etc)
      }

    });
    items.push('</ul>');                             // close </ul>
  }                                                  // end recursion function


  recurse(data);            // call recursion
  return items.join('');    // return results
}  // end buildULfromJSON()

Here is snippet of example output where the tweet contains one hashtag, two user mentions, and three urls. Those items are returned as a comma seperated list of objects (json data). I’m at a complete loss how to tackle this. Any direction you guys could provide would be excellent.

Note the [text] string. It helps to put the [entities] section in context.

<snippet>
[text] = "#Hashtag @PithyTwits @LuvsIt2 http://link1.com http://link2.com http://link3.com"
[retweet_count] = 0
[entities] =>
    [hashtags] =>
        [0] =>
            [text] = "Hashtag"
            [indices] = 0,8
    [user_mentions] = [object Object],[object Object]
    [urls] = [object Object],[object Object],[object Object]
[in_reply_to_screen_name] = null
[in_reply_to_status_id_str] = null
</snippet>

My big issue at this point is that I don’t know how to test for these lists without giving jquery.js fits. Once I do know how to isolate these lists in code, string functions for dealing with comma separated lists don’t sound like the perfect fit… Any advice would be welcomed.

Thank You,

Skip

  • 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-21T12:49:27+00:00Added an answer on May 21, 2026 at 12:49 pm

    OK, here’s the skinny. It’s an Array!

    $.isArray(val) will return true and identify it as such, and it can be iterated with $.each(); just like other objects.

    It is a valid object structure, from valid JSON, and can be avoided by using the JSON_FORCE_OBJECT option in PHP’s json_encode(); function. For my needs it is better to not force the object because I’m also dealing with arrays of integers I want returned on a single line.

    For my needs I changed the first if() in my recusion function from this…

    if((val == '[object Object]') && (typeof val == 'object')){
    

    to this…

    if((val != null) && (typeof val == 'object') &&
        ((val == '[object Object]') || (val[0] == '[object Object]'))){
    

    That will match objects, or arrays of objects, then send them back to resurse();

    Oddly, Javascript complains when val is null and we test against val[0]. I guess it probably makes sense, because you aren’t just testing against the value, your also trying to dive into the null object.

    Thanks for your attention, I figured out my issue, and I’ve now got an account on Stackoverflow. It’s a win-win-win!

    Thanks again.

    Skip


    Here is the revised buildULfromOBJ(); function…

    function buildULfromOBJ(obj){
      var fragments = [];
    
      //declare recursion function
      function recurse(item){
        fragments.push('<ul>'); // start a new <ul>
    
        $.each(item, function(key, val) {  // iterate through items.
    
          if((val != null) && (typeof val == 'object') &&   // catch nested objects
                   ((val == '[object Object]') || (val[0] == '[object Object]'))){
    
            fragments.push('<li>[' + key + '] =></li>'); // add '[key] =>'
            recurse(val);            // call recurse to add a nested <ul>
    
          }else if(typeof(val)=='string'){  // catch strings, add double quotes
    
            fragments.push('<li>[' + key + '] = \"' + val + '\"</li>');
    
          }else if($.isArray(val)){         // catch arrays add [brackets]
    
            fragments.push('<li>[' + key + '] = [' + val + ']</li>');
    
          }else{                            // default: just print it.
    
            fragments.push('<li>[' + key + '] = ' + val + '</li>'); 
          }
        });
        fragments.push('</ul>'); // close </ul>
      }
      // end recursion function
    
      recurse(obj);            // call recursion
      return fragments.join('');    // return results
    }  // end buildULfromJSON()
    

    The top two elses are simply to make pretty output, and help deliniate between strings and literals. They can be removed to streamline the flow.

    Here is the same snippet I posted earlier, properly formatted this time…

    <snippet>
    [text] = "#Hashtag @PithyTwits @LuvsIt2 http://link1.com http://link2.com http://link3.com"
    [retweet_count] = 0
    [entities] =>
        [hashtags] =>
            [0] =>
                [text] = "Hashtag"
                [indices] = [0,8]
        [user_mentions] =>
            [0] =>
                [indices] = [9,20]
                [screen_name] = "PithyTwits"
                [name] = "[object Object]"
                [id_str] = "258175966"
                [id] = 258175966
            [1] =>
                [indices] = [21,29]
                [screen_name] = "LuvsIt2"
                [name] = "Strictly Indifferent"
                [id_str] = "255883555"
                [id] = 255883555
        [urls] =>
            [0] =>
                [indices] = [30,46]
                [url] = "http://link1.com"
                [expanded_url] = null
            [1] =>
                [indices] = [47,63]
                [url] = "http://link2.com"
                [expanded_url] = null
            [2] =>
                [indices] = [64,80]
                [url] = "http://link3.com"
                [expanded_url] = null
    [in_reply_to_screen_name] = null
    </snippet>
    

    Notice that [entities][user_mentions][0][name] = “[object Object]” I stuck that in to ensure that string values don’t break the code. Also notice the [indices] items. Those are the arrays I prefer to see on a single line (I get anal over the stupidest stuff 🙂 )

    Thanks again!

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

Sidebar

Related Questions

No related questions found

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.