-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.

6
  • What tool and programming language do you use? Is it Javascript? Commented Dec 19, 2017 at 20:25
  • Can you share some more code? What language do you use? Commented Dec 19, 2017 at 20:26
  • there is no semicolon after pictures is it ok in your orginal code? Commented Dec 19, 2017 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. Commented Dec 19, 2017 at 20:27
  • 1
    Possible duplicate of How to access nested JSON data Commented Dec 19, 2017 at 20:33

3 Answers 3

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 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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 :)

1 Comment

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.
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.