Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
VueJS - Composition API - How to properly import and read a local JSON file

I am having some trouble loading in a local JSON file to read some data. It seems I can get the data to import as I can log it out to the console. If I do console console.log(rooms.value) for example I get the following in chrome browser:

enter image description here

I want to iterate through the data, however if I try a v-for="room in rooms" and output room.id for example I just get enter image description here

Please can someone help?

JSON:

{
    "rooms":[
        {
            "id": 1,
            "name": "Master Bedroom",
            "floor": 1,
            "lights": true,
            "blinds": true,
            "climate": true,
            "music": true
        },
        {
            "id": 2,
            "name": "Living Room",
            "floor": 0,
            "lights": true,
            "blinds": true,
            "climate": true,
            "music": true
        }
    ]
}

Vue:

<script>
import { ref } from 'vue'
import roomData from '../../data/db.json'

export default {
    components: {},

    setup(){
        const rooms = ref(roomData)

        console.log(rooms.value)

        return { rooms }
    }

}
</script>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can call result of iteration with room.id not rooms.id, this might just be a typo in your template code.

about 4 years ago · Juan Pablo Isaza Report

0

I've worked it out - I could only access the array by assigning the constant to roomData.rooms (as opposed to just roomData). So working code looks like:

<script>
import { ref } from 'vue'
import roomData from '../../data/db.json'

export default {
    components: {},

    setup(){
        const rooms = ref(roomData.rooms)

        console.log(rooms.value)

        return { rooms }
    }

}
</script>
about 4 years ago · Juan Pablo Isaza Report

0

to import your json you can do this with SFC in vuejs3

  <script setup>
    import roomData from '../../data/db.json'
    
    console.log(roomData)
    </script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!