0

I'm trying to cleanse some data and place an array of objects inside of an object. Here's my array of objects.

careerStats:

[
  {
    gp: '35',
    gs: '34',
    mpg: '33.2',
    fg: '.515',
    tp: '1.5',
    ft: '1.4',
    rpg: '0.5',
    apg: '0.7',
    bpg: '.692',
    spg: '0.7',
    ppg: '3.6'
  },
  {
    gp: '22',
    gs: '22',
    mpg: '36.7',
    fg: '.504',
    tp: '5.5',
    ft: '2.4',
    rpg: '1.7',
    apg: '2.0',
    bpg: '.822',
    spg: '1.5',
    ppg: '6.5'
  },
  {
    gp: '57',
    gs: '56',
    mpg: '34.6',
    fg: '.509',
    tp: '3.1',
    ft: '1.8',
    rpg: '1.0',
    apg: '1.2',
    bpg: '.775',
    spg: '1.0',
    ppg: '4.7'
  }
]

And here's how I'm placing the careerStats array of objects inside of a player array.

player:

player.push(
    {
        name: "",
        image: "",
        position: "",
        description: "",
        dob: "",
        hometown: "",
        country: "",
        height_feet: "",
        height_inches: "",
        weight: "",
        season: careerStats
    });

This does not return any sort of error; however, when I log the player to the console here's what displays:

[
  {
    name: '',
    image: '',
    position: '',
    description: '',
    dob: '',
    hometown: '',
    country: '',
    height_feet: '',
    height_inches: '',
    weight: '',
    season: [ [Object], [Object], [Object] ]
  }
]

Since I know the data is fine when it's in the careerStats array, this leads me to two questions:

  1. Am I doing something incorrectly to receive this output as [ [Object], [Object], [Object] ]? ANSWER: Resolved by changing to console.log(JSON.stringify(player))
  2. If this is the expected output and the data is indeed there, how can I write this to a JSON file where the data displays instead of "[object Object]"?

        JSON.stringify(player);
        fs.appendFile('myjsonfile.json', player, function (err) {
            if (err) throw err;
            console.log('Saved!');
        });
    

Thanks in advance for the help!

5
  • You can click on the objects in the console to expand them Commented May 11, 2020 at 16:17
  • 2
    Try console.log(JSON.stringify(player)) you should see what you expect. Commented May 11, 2020 at 16:17
  • 2
    console.log will only recurse into an array or object so many levels before it prints generic type strings. This is so you can print complex objects with deep levels of nested data without it taking up thousands of lines when all you wanted was to view some of the top-level fields. Commented May 11, 2020 at 16:17
  • @Barmar - thank you for the information, I did not know he limits to console.log! Commented May 11, 2020 at 16:50
  • @Abion47 - same to you, I learned something valuable this morning. Commented May 11, 2020 at 16:50

2 Answers 2

1

To write to a JSON file in Node then perhaps use node filesystem

  1. Convert the object to JSON using JSON.stringify()

    const fs = require('fs'); // Load in filesystem
    const playerJSON = JSON.stringify(player);
    
  2. Create JSON file using fs.writeFileSync() - The below will create a JSON file in your directory called player.json with player data stored as JSON

    fs.writeFileSync('player.json', playerJSON);
    
Sign up to request clarification or add additional context in comments.

Comments

1

console.log() will only show up to 2 levels of your object's structure, anything else will be "collapsed" like that.

You could use console.dir():

console.dir(player, { depth: null });

Or util.inspect:

const util = require('util')

console.log(util.inspect(player, { depth: null });

Or JSON.stringify():

console.log(JSON.stringify(player, null, 2));

However, note JSON.stringify() has some limitations like not being able to handle circular references or not displaying functions at all.

I would go for console.dir(), as it already uses util.inspect internally, as stated in the docs:

Uses util.inspect() on obj and prints the resulting string to stdout. j.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.