1

I have been referencing to this post Access / process (nested) objects, arrays or JSON, put have had no luck in figuring this out:

i have this JSON that I get from a http request: and in my app I need to asks the Posts object.

    { 
      "email" : "[email protected]", 
      "username" : "rambo",
      "fullname" : "Michael Stalone",
      "posts" : [
                 { "username" : "Bad Man",  
                   "comments" : 
                      [ 
                         { "com_user" : "michael", "com_post" : "good stuff" },
                         { "com_user" : "alex", "com_post" : "hell yes" }  
                      ]  
                  }, 
                  { "username" : "CheckerTats", 
                    "comments" : [ 
                          { "com_user" : "basky", "com_post" : "wow awesome" } 
                      ] 
                  }
                ]
      }

I get it fine and see everything as it should be. I also notice it returns and array object so i index the varriable i assign it to: var items = data[0]. console.log(data[0]) shows this:

{
 "email":"[email protected]",
 "username":"rambo",
  "fullname":"Michael Stalone",
  "posts":["[object Object]","[object Object]"]
}

So this is all good and dandy and realize I need to take it a step further and investigate data[0]['posts'][0] which shows to be:

[object Object]

I am now almost certain that this is the first post object. I would think that data[0]['posts'][0]['username'] would give me that particular user name. To make matters worse my dev environment is an iOS Apache Cordova app, so when I run console.log on it I am returned nothing.I have assigned this to variables, uses stringify, dot notation, and am still continuously not able to access this object. I need to do so to assign it to an angular scope variable.

EDIT:

A new log is now returning undefined:

 var items = dat[0]['posts'][0];
 console.log(items['username']); // undefined
12
  • Have you tried console.dir() instead of console.log()? does it show anything different? Also, what is the backend? do you have an http intercept defined? The only thing I can think of at the moment, if you can't get those objects to expand using console.dir(), is that somehow there is a JSON.toString (not stringify()) on those objects somewhere in the process. Seems unlikey, but ?? Commented Dec 5, 2014 at 6:12
  • In your log there is extra symbols ," after fullname. This can not be an object. Where and how did you create this JSON? Commented Dec 5, 2014 at 6:20
  • @Beartums I did use console.dir() and it responded the same. , right now my backend is NodeJS and I use the latest moongoose scheme mongoosejs.com/docs/guide.html for parsing. But the first json listed is exactly the json in the MongoDB database. So I dont see how it can be changing Commented Dec 5, 2014 at 6:21
  • @Epsilon sorry that was a typo Commented Dec 5, 2014 at 6:23
  • @Epsilon as I said in an above comment, I use moongoose on the Node JS side for parsing. And the first JSON I listed is exactly how it is in the database. My app is angular and it uses http which gives me back a json object I don't have to parse on client side. Commented Dec 5, 2014 at 6:27

2 Answers 2

3

look closely at data[0]: posts contains an array of two strings that say "[object Object]", not two objects. How are you parsing the json?

oh, and your syntax is correct for extracting the username from the nested objects

Sign up to request clarification or add additional context in comments.

3 Comments

My guess is that he's calling toString somewhere on each actual post object.
Andras you were correct, I will create an answer with explanation
THIS was the correct answer. In my mongoDB, I remembered I had manually entered what I thought was correct data into my "posts" array in that JSON. All looked well in mongoDB (as I posted in my question) however I guess by manually inputting I did not write the syntax right.
0

if variable dat holds your json object. Then you can try below code to access username in your json:

dat.posts[0].username

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.