Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaning up json includes and usage

I am importing data from json like this

import data from './data.json'

where data.json looks somewhat similar to this

{
  "data": [
    {"title": "Some title", "text": "Some text"},
    {"title": "Some title", "text": "Some text"},
    {"title": "Some title", "text": "Some text"}
  ]
}

so to use it in my file I'd usually do something like data.data[0].title which in my opinion is not the cleanest way, ideally I'd like to use it like data[0].title is there a way I can include or edit my json file to acheive this?

like image 658
Ilja Avatar asked May 16 '16 10:05

Ilja


1 Answers

You can import {data} from './data.json. That will only import that data key from the object, and place it into a variable called data.

like image 98
Madara's Ghost Avatar answered Sep 21 '22 14:09

Madara's Ghost