简体   繁体   中英

How do I get a JSON file to show sub-arrays?

I know it's a simple question, but I've tried multiple ways and I can't get it to work. I have a huge JSON file (that I use a reduced version of) which I finally managed to let my script retrieve the file using the require(); function:

let wordBank = './Wordbank.json';

function getData() {
  console.log(require(wordBank));
}
getData();

The terminal displays the few lines of my reduced JSON file like this, why?: Notice the green "Array"?

I haven't really been able to find any answer...

Use console.dir instead of console.log

console.dir(data, {depth:100})

Its having a depth parameter to expand json upto given level

You need to use JSON.stringify to convert the object to JSON string. So you need to change your code like this:

let wordBank = './Wordbank.json';
function getData() {
   console.log(JSON.stringify(require(wordBank)));
}
getData();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM