0

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" : "test@email.com", 
      "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":"test@email.com",
 "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
Community
  • 1
  • 1
Michael Ramos
  • 5,523
  • 8
  • 36
  • 51
  • 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 ?? – Beartums Dec 05 '14 at 06:12
  • In your log there is extra symbols `,"` after `fullname`. This can not be an object. Where and how did you create this JSON? – nikodz Dec 05 '14 at 06: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 http://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 – Michael Ramos Dec 05 '14 at 06:21
  • @Epsilon sorry that was a typo – Michael Ramos Dec 05 '14 at 06: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. – Michael Ramos Dec 05 '14 at 06:27
  • About 'EDIT', I believe `dat[0]` is still typo. – nikodz Dec 05 '14 at 06:28
  • @MiguelSamor, maybe the Mongoose schema? Any chance Mongoose thinks those objects are strings? Following on the comment by Epsilon, you might want to look into your MongoDB. I used to use something calle -- I believe -- roboMongo to look through the database. That might give you and idea. – Beartums Dec 05 '14 at 06:29
  • @MiguelSamor Then you should see the place, where you wrote it into mongo, because they are strings `[object Object]` instead of real objects. – nikodz Dec 05 '14 at 06:30
  • Your JSON is legal, what parser you are using? – Leo Dec 05 '14 at 06:30
  • It's returning `undefined` because `items` is string '[object Object]' and and therefore `items['username']` is nothing. – nikodz Dec 05 '14 at 06:35
  • @Epsilon I am currently restructuring my server code, will come back with response shortly. – Michael Ramos Dec 05 '14 at 06:57
  • @Epsilon My server is now written properly, I will explain in an answer to this question. Thank you for you input – Michael Ramos Dec 05 '14 at 08:43

2 Answers2

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

Andras
  • 2,995
  • 11
  • 17
  • My guess is that he's calling `toString` somewhere on each actual post object. – Vinay Dec 05 '14 at 07:34
  • Andras you were correct, I will create an answer with explanation – Michael Ramos Dec 05 '14 at 08:42
  • 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. – Michael Ramos Dec 08 '14 at 19:40
0

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

dat.posts[0].username
Madhura KM
  • 140
  • 1
  • 10