0

How would I read deep JSON data nested deep inside a file? I've tried different methods and can't seem to get this to work.

<template>
    <div>
        <div v-for="edu in info" :key="edu">

            <div>{{ edu.section.title }}</div> // this is what im trying to get to work
        </div>

        <div class="card container">
            {{ info }}
        </div>
    </div>
</template>

<script>
    import axios from 'axios';

    export default {
        data() {
            return {
                info: null
            }
        },
        mounted() {
            axios
                .get('./calculus.json') // Data Below
                .then(response => (this.info = response.data.edu))
                .catch(error => console.log(error))
        }
    }
</script>

My JSON looks like this:

{
    "edu": {
        "1": {
            "title": "Title One",
            "subtitle": "Subtitle One",
            "description": "Description One",
            "section": {
                "1": {
                    "title": "Section One Title",
                    "content": "Section One Content"
                }
            }
        },
        "2": {
            "title": "Title Two",
            "subtitle": "Subtitle Two",
            "description": "Description Two",
            "section": {
                "1": {
                    "title": "Section One Title",
                    "content": "Section One Content"
                }
            }
        }
    }
}

How can I use vue-for and get the data inside the section to get it to display under the title? For example: title, section>title, section>subtitle, etc.

2 Answers 2

1

Given each section is also an object with weird numeric keys, you can iterate them in the same way you do info.

I would also advise you to use identifiable values instead of the entire edu object in your :key bindings.

<div v-for="(edu, eduId) in info" :key="eduId">
  <div v-for="(section, sectionId) in edu.section" :key="sectionId">
    {{ section.title }}
  </div>
</div>

If possible, I would alter the format of your JSON data to use actual arrays instead of objects with numeric keys.

Sign up to request clarification or add additional context in comments.

Comments

1

One way to browse your object deeply is to cumulate v-for on your object (and children) entries.

ie:

<div v-for="([category, books], catkey) in Object.entries(info)" :key="`category-${catkey}`">
    <div>{{ category }} :</div>
    <div v-for="([num, book], numkey) in Object.entries(books)" :key=`book-${catkey}-${numkey}`>
        <div v-for="([field, value], valkey) in Object.entries(book)" :key=`field-${catkey}-${numkey}-${valkey}`>
            {{ field }} : {{ value }}
        </div>
    </div>
</div>

If you find it too verbose, you may try to flatten your computed data to have the following structure:

[
    {
        "category": "edu",
        "id": "1",
        "title": "Title One",
        "subtitle": "Subtitle One",
        "description": "Description One",
        "section": {
            "1": {
                "title": "Section One Title",
                "content": "Section One Content"
            }
        }
    }
]

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.