'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...
Solution 1:[1]
Use console.dir instead of console.log
console.dir(data, {depth:100})
Its having a depth parameter to expand json upto given level
Solution 2:[2]
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();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harsh Rohila |
| Solution 2 | Tam Dao |
