110

How to access JSON objects in the vue.js app I am new in this

import json from './json/data.json'

the JSON file is loaded and now I have to access the objects within it

Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
Haroon Aslam
  • 1,593
  • 4
  • 11
  • 20

4 Answers4

194

Assign the import to a data property

<script>
      import json from './json/data.json'
      export default{
          data(){
              return{
                  myJson: json
              }
          }
      }
</script>

then loop through the myJson property in your template using v-for

<template>
    <div>
        <div v-for="data in myJson">{{data}}</div>
    </div>
</template>

NOTE

If the object you want to import is static i.e does not change then assigning it to a data property would make no sense as it does not need to be reactive.

Vue converts all the properties in the data option to getters/setters for the properties to be reactive. So it would be unnecessary and overhead for vue to setup getters/setters for data which is not going to change. See Reactivity in depth.

So you can create a custom option as follows:

<script>
          import MY_JSON from './json/data.json'
          export default{
              //custom option named myJson
                 myJson: MY_JSON
          }
    </script>

then loop through the custom option in your template using $options:

<template>
        <div>
            <div v-for="data in $options.myJson">{{data}}</div>
        </div>
    </template>
Kalnode
  • 9,386
  • 3
  • 34
  • 62
Vamsi Krishna
  • 30,568
  • 8
  • 70
  • 78
  • @HaroonAslam then how do you want it? – Vamsi Krishna Aug 08 '17 at 12:44
  • @HaroonAslam show your json data so helping you would be easier – Vamsi Krishna Aug 08 '17 at 12:47
  • http://jsoneditoronline.org/?id=4b5f37eec8bce13bcb21bd86ad7fe8d5 @Vamsi – Haroon Aslam Aug 08 '17 at 12:50
  • 2
    Great detail on the `$options` approach, I'd been using `created()` for non-reactive data - this is better – Kong Dec 11 '19 at 19:45
  • What is root dir? – mathtick Dec 31 '20 at 13:57
  • Also this just doesn't work. It should not be market correct. – mathtick Dec 31 '20 at 14:23
  • @mathtick I'm kinda late to the party, but it seems, that the root directory is the directory the current view is in. So I was trying to do that in my `src/views/Component.vue`. My JSON file is `src/mock-data/data.json`. Using `import data from '../mock-data/data.json'` (relative path) worked for me. I am a newbie in regards to Vue, so I assume I'll be moving these things into some service or something, but it is okay for now. – Igor Nov 21 '21 at 23:56
18

If your file looks like this:

[
    {
        "firstname": "toto",
        "lastname": "titi"
    },
    {
        "firstname": "toto2",
        "lastname": "titi2"
    },
]

You can do:

import json from './json/data.json';
// ....
json.forEach(x => { console.log(x.firstname, x.lastname); });
smarber
  • 4,829
  • 7
  • 37
  • 78
16

Typescript projects (I have typescript in SFC vue components), need to set resolveJsonModule compiler option to true.

In tsconfig.json:

{
  "compilerOptions": {
    ...
    "resolveJsonModule": true,
    ...
  },
  ...
}

Happy coding :)

(Source https://www.typescriptlang.org/docs/handbook/compiler-options.html)

Luckylooke
  • 4,061
  • 4
  • 36
  • 49
0

I have recently started working on a project using Vue JS, JSON Schema. I am trying to access nested JSON Objects from a JSON Schema file in the Vue app. I tried the below code and now I can load different JSON objects inside different Vue template tags. In the script tag add the below code

import  {JsonObject1name, JsonObject2name} from 'your Json file path';

Now you can access JsonObject1,2 names in data section of export default part as below:

data: () => ({ 
  
  schema: JsonObject1name,
  schema1: JsonObject2name,   
  
  model: {} 
}),

Now you can load the schema, schema1 data inside Vue template according to your requirement. See below code for example :

      <SchemaForm id="unique name representing your Json object1" class="form"  v-model="model" :schema="schema" :components="components">
      </SchemaForm>  

      <SchemaForm id="unique name representing your Json object2" class="form" v-model="model" :schema="schema1" :components="components">
      </SchemaForm>

SchemaForm is the local variable name for @formSchema/native library. I have implemented the data of different JSON objects through forms in different CSS tabs.

I hope this answer helps someone. I can help if there are any questions.

Amin.MasterkinG
  • 805
  • 1
  • 12
  • 23