0

I have a Javascript code that uses an array of objects as:

var words = [{"text":"This", "url":"http://google.com/"},
         {"text":"is", "url":"http://bing.com/"},
         {"text":"some", "url":"http://somewhere.com/"},
         {"text":"random", "url":"http://random.org/"},
         {"text":"text", "url":"http://text.com/"},
         {"text":"InCoMobi", "url":"http://incomobi.com/"},
         {"text":"Yahoo", "url":"http://yahoo.com/"}]

Then I use words in the rest of code and everything works fine.

Then I store the data in a JSON file, lets call the file "myfile.json" its content is:

{
"words": [
{"text":"This", "url":"http://google.com/"},
{"text":"is", "url":"http://bing.com/"},
{"text":"random", "url":"http://random.org/"},
{"text":"some", "url":"http://somewhere.com/"},
{"text":"text", "url":"http://text.com/"},
{"text":"InCoMobi", "url":"http://incomobi.com/"},
{"text":"Yahoo", "url":"http://yahoo.com/"}
]
}

I load this file using d3.json as:

d3.json("myfile.json", function(words) {
    console.log(words);  //Log output to console
});

And then I use words the same as before, now my code does not work! What is the difference between the two things and how can I fix the second method that I load the file?

4
  • 2
    the 1st words is an array which contains object, the 2nd words is an object that contains a property words which is an array of objects. Commented Apr 20, 2014 at 15:12
  • How can I convert the 2nd words to something similar to 1st? Commented Apr 20, 2014 at 15:13
  • Use words.words to get the same result. Commented Apr 20, 2014 at 15:14
  • it is a little confusing. Assume instead of { "words": [ ... I had {"X": [... then what would I use? Commented Apr 20, 2014 at 15:17

2 Answers 2

1

The difference between the two is that the first is an array, whereas the second is an object with one property words containing the the array of objects in the first example.

To convert the second to the same as the first simply do:

d3.json("myfile.json", function(words) {

    words = words.words;
    console.log(words);  //Log output to console
});
Sign up to request clarification or add additional context in comments.

5 Comments

I thought it is the same, but when I used what you said my 2nd method is still not working as 1st. Is there any other difference that you can think of?
This should be working. I suspect your data is not what you say. In the function, show the output of console.log(JSON.stringify(words)).
Here is what I get: [{"text":"This","url":"http://google.com/"},{"text":"is","url":"http://bing.com/"},{"text":"some","url":"http://somewhere.com/"},{"text":"random","url":"http://random.org/"},{"text":"text","url":"http://text.com/"},{"text":"InCoMobi","url":"http://incomobi.com/"},{"text":"Yahoo","url":"http://yahoo.com/"}]
That's exactly the same as your first example. It's an array of objects so either iterate over it, or directly access the objects e.g. words[0].text
I got a clue: when I do console.log(words); outside of d3.json(... then I get array[0] but when I do it inside I get array[7]. Looks like words is not defined outside of d3.json(... ?
1

Like I said in my comment "the 1st words is an array which contains object, the 2nd words is an object that contains a property words which is an array of objects".

You want to use the words property of your object, you can do this like the example below.

d3.json("myfile.json", function(result) {
    console.log(result.words); 
});

1 Comment

I thought it is the same, but when I used what you said my 2nd method is still not working as 1st.

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.