0

I'm having trouble iterating over data from a json file in Vue JS. I've got everything set up and have access to the json. What am I missing here:

JSON

{
  "test": "example",
  "data": [
    {
      "name": "My Name",
      "address": "My Address"
    }
  ]
}

Vue JS html

{{ someData['name'] }}

<ul>
  <li v-for="item in someData" v-bind:key="item.id">
    {{ item }}
  </li>
</ul>

and my script:

created() {
    this.$http.get('http://localhost:3000/properties.json').then(response => {
      // get body data
      this.someData = response.body
    }, response => {
      // error callback
    });
  }

The result I'm getting is:

Result

As you can see, in my v-for simply specifying item grabs each of the items here, but I want to be able to do something like {{ item['name'] }} and {{ item['address'] }}

2
  • Please, specify what does someData refer to Commented Sep 20, 2018 at 10:46
  • Hi, someData is just for testing purposes. I've just updated my question above with some new JSON data as I'm having trouble with that part Commented Sep 20, 2018 at 11:04

3 Answers 3

2

The main problem is in your JSON file. VueJS v-for is iterating over the keys in your JSON file, in this case {{ item }} will be getting name and address. That is the reason of your output.

To solve this...

Format your JSON file as

[{name: 'John', address: 'Jane'}]

Doing this your VueJS v-for will be iterating over JSON array and {{ item }} will get the value { name: 'John', address: 'Jane' } and now you can do {{ item.name }} {{ item.address }}

An example here

EDIT: Update your API request code

created() {
    this.$http.get('http://localhost:3000/properties.json').then(response => {
      // get body data
      this.someData = [response.body]
    }, response => {
      // error callback
    });
  }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but my json is going to be coming from an API, or a json file, how would I adapt this?
Awesome! Thank you so much. it's just occurred to me that my data is going to contain multiples like this: ``` { "test": "example", "data": [ { "name": "My Name", "address": "My Address" } ] } ``` So now, I tried doing: {{ item.data.name }} and that didn't work?
any suggestions?
with that JSON modification you must update your created() method to fit what you want. In this case, without viewing the code, I think you will add this. this.someData = response.body.data.
0

You can simply do the following:

<template v-for="(item, key) in someData">
  <span v-if="key == 'address'">{{item}}</span>
</template>

Comments

0

The crucial point is that the v-for="item in someData" part in the component's view iterates over values of the someData object, namely "John" and "Jane".

Depending on what exactly is your goal, you should either reformat the JSON to be an array of objects (as suggested by @F.bernal), or change the logic of v-for directive.

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.