0

I have problems to access nested data from my api call. The JSON response looks like this: enter image description here

I try to access the attribute_name in attributes. And the language_name in languages. I can display the location_name with the following code {{ userdetails.location.location_name }}


I have always problems with nested api data. Is there a guide how to access data from an api?

Thank you!

Update: My code looks like this

data() {
    return {
      userdetails: undefined,
    };
  },

  methods: {
    getUserData() {
      DataService.getUserById()
        .then((response) => {
          this.userdetails = response.data;
          console.log(response);
        })
        .catch((error) => {
          console.log(
            "Ein Fehler beim User ist aufgetreten: " + error.response
          );
        });
    },
  },
  created() {
    this.getUserData();
  },
};
2
  • The response is saved in any variable? I supose you save the res.data in userdetails? Can you show us the code? Commented Dec 2, 2021 at 8:13
  • @MuXeD I added my code to the question Commented Dec 2, 2021 at 8:24

1 Answer 1

1

attributes property is array so try (index is index in array):

{{ userdetails.attributes[index].attribute_name }}

and if you want to show them all:

<div v-for="(attr, i) in userdetails.attributes" :key="i" >
  {{ attr.attribute_name }}
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I had to add another "attribute" but it works now perfectly. <div v-for="(attr, i) in userdetails.attributes" :key="i"> {{ attr.attribute.attribute_name }} </div>
nice, cheers :)

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.