0

I wrote a quick script to parse two fairly large json file (~17k records) to do a comparison of the two. I have confirmed they are both valid json (via jsonlintpro) and the same format. (The source is the same so this should be a given. But, I always assume the mistake is mine. And I still do. Just somewhere else.) However, the parsed file just outputs [object, Object]. I'm wondering what the cause could possibly be?

The json format is like this small snippet (anonymized of course):

[
{
    "id": "1234",
    "name": "Name1",
    "url": "https://localhost/Name1",
    "date_created": "2013-07-05T18:47:05Z",
    "date_cancelled": "",
    "props": [
        {
            "id": "54321",
            "type": "Client",
            "value": "General Store"
        },
        {
            "id": "65432",
            "type": "Contact_Name",
            "value": "Joe Smith"
        }
    ]
},
{
    "id": "23456",
    "name": "Name2",
    "url": "https://localhost/Name2",
    "date_created": "2014-02-27T17:46:43Z",
    "date_cancelled": "",
    "props": [
        {
            "id": "34567",
            "type": "Client",
            "value": "Bait Shop"
        }
    ]
}]

And here is the pertinent code:

var _ = require('underscore');
var recs = require('./prod.json');

printArr(recs);

console.log(recs.length);

function printArr(arr) {
        arr.forEach(function(item) {
                console.log(item + ", ");
        });
}

Any guidance would be greatly appreciated.

UPDATE:

Ok, so apparently the issue is with my printArr function. I'm not sure what I'm doing wrong there. I'd like to figure it out because I want to expand upon that so I can print selectively.

3
  • does console.log(recs.length); output a non-zero number? Commented Apr 3, 2014 at 20:09
  • Yes, it outputs 17005. Commented Apr 3, 2014 at 20:10
  • 1
    Concatenating an Object forces it to a String, which will result in "[object Object]". By passing each Object as its own argument, console.log(item), they'll be inspected and rendered in a more useful format. Commented Apr 3, 2014 at 20:25

3 Answers 3

3

the parsed file just outputs [object, Object].

This is the expected behavior BECAUSE you are concatenating an object with a string.

Try console.log(item) instead

Sign up to request clarification or add additional context in comments.

Comments

0

console.log(item); should indeed print [object, Object], did you try to output its properties instead?

function printArr(arr) {
    arr.forEach(function(item) {
        console.log( item.id, item.name, item.url, item.date_created, item.date_cancelled, item.props, ';');
    });
}

Comments

0

Just export the value from the prod.json file.

prod.json file

module.exports = [
{
  "id": "1234",
  "name": "Name1"
},

{
  "id": "1234",
  "name": "Name1"

}]

elsewhere

var recs = require('./prod.json')
console.log(recs)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.