-3

I'm working on an app in javascript, and I have this json object (this is a simplified example of the actual json object)

{
  "data": [
    "user": {
      "pictures"{
        "sizes"[
          0: {
            "link": "http://www"
          },
          1: {
            "link": "http://"
          }
        ]
      }
    }
  ]
}

And I want to get the value of link, so i tried data.user.pictures.sizes[0].link, and it returned an error. How to get the right value?

edit: I'm using a map function to loop around the data object, so i can display values in an html page. it works for most of the other items, except for pictures sizes, i cant seem to get the value of the second item in the sizes array.

Ndx
  • 517
  • 2
  • 12
  • 29
  • What tool and programming language do you use? Is it Javascript? – lospejos Dec 19 '17 at 20:25
  • Can you share some more code? What language do you use? – iep Dec 19 '17 at 20:26
  • there is no semicolon after pictures is it ok in your orginal code? – Bakhrom Rakhmonov Dec 19 '17 at 20:26
  • I'm using javascript, and mapping over the json object to display the json object in an html page. Sorry about the json format, its not the original one, i just tried to make a copy of it. – Ndx Dec 19 '17 at 20:27
  • 1
    Possible duplicate of [How to access nested JSON data](https://stackoverflow.com/questions/9463233/how-to-access-nested-json-data) – devlin carnate Dec 19 '17 at 20:33
  • it's not a duplicate. It shows getting values from nested objects of json obj. in my situation, there is an array within an object that i only need one array item from it. – Ndx Dec 19 '17 at 20:39

3 Answers3

3

From the data, it shows that 'data' contains array and sizes contains array that contains object of properties 0 and 1 , so do this

data[0].user.pictures.sizes[0].0.link
Lolu Omosewo
  • 263
  • 1
  • 4
  • I'm already using a map function to loop around the object, but in picture.sizes I only want to value of the second array item. – Ndx Dec 19 '17 at 20:28
3

First of all you need to have colons to "pictures" and "sizes" :)

Secondly, it depends what your structure is.

for example:

if you need to use arrays as they appear in your example:

var a = {
    "data": [
        {
            "user": {
                "pictures": {
                    "sizes": [
                        {
                            0: {
                                "link": "http://www"
                            }
                        },
                        {
                            1: {
                                "link": "http://"
                            }
                        }
                    ]
                }
            }
        }
    ]
}

console.log(a.data[0].user.pictures.sizes[0][0].link);

or you just need a json without arrays in it:

var b = {
    "data": {
        "user": {
            "pictures": {
                "sizes": {
                    0: {
                        "link": "http://www"
                    },
                    1: {
                        "link": "http://"
                    }
                }
            }
        }
    }
}

console.log(b.data.user.pictures.sizes[0].link);

or you can even mix them:

var c = {
    "data": {
        "user": {
            "pictures": {
                "sizes": [
                    {
                        "link": "http://www"
                    },
                    {
                        "link": "http://"
                    }
                ]
            }
        }
    }
}

console.log(c.data.user.pictures.sizes[0].link);

notice the subtle difference of "sizes" in b and c and the double array of a. This is because json with index as keys is the same as an array. check this example:

var example = [
    {
        0: "example00",
        1: "example01",
        2: "example02"
    },
    {
        0: "example10",
        1: "example11",
        2: "example12"
    },
]

console.log(example[0][1]); // == example01

you can see that you have 2 arrays within the example array

hope that helps :)

  • it helped. thanks! it seems the real issue wasn't the path of the object but rather having a link with value null which rendered an error and couldnt display the rest of the data. – Ndx Dec 19 '17 at 20:57
0

Your JSON is wrong, it should be as below :

var js = {  
   "data": {  
      "user":{  
         "pictures":{  
            "sizes":[  
               {  
                  "link":"http://www"
               },
               {  
                  "link":"http://"
               }
            ]
         }
      }
   }
}

To get the value:

js.data.user.pictures.sizes[0].link
Shiko
  • 2,448
  • 1
  • 24
  • 31