4

Not able to show deeply nested JSON Objects to show up.Have been looking at all kinds of stackoverflow posts for this. Appreciate any help on this newbie question. I want it to show details of the athlete JSONObject within the athletes array. It shows up like [Object].

eventUnitResults: [ { is_team: true, athletes: [ [Object], [Object] ] },
  { is_team: true, athletes: [ [Object], [Object] ] } ]

const result = {}
let eventUnitResults = [];
let athletes = [];

for (i=0; i < 2; i++) {
  const athlete = {};
  athlete.athlete_name = 'Ram' + i;
  athlete.athlete_gender = 'M'
  athletes.push(athlete);
}
for (j=0;j < 2;j++) {
  const nestedResult = {};
  nestedResult.is_team = true;
  if (athletes) {
    nestedResult.athletes = athletes;
  }
  console.log('nestedResult:', nestedResult);
  if (nestedResult) {
    eventUnitResults.push(nestedResult);//TODO:
    //eventUnitResults.push(JSON.stringify(nestedResult));//TODO:
  }
}
console.log('eventUnitResults:', eventUnitResults);//<==== how can I get deeply nested values of athletes showing up properly here

if (eventUnitResults) {
  result.event_unit_results = eventUnitResults;
}
console.log('result:', result)

TIA

Vijay
  • 595
  • 1
  • 13
  • 27
  • what does the original JSON look like: hint, show the **string** before you parse it – Jaromanda X Nov 14 '17 at 07:31
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. *"It shows up like [Object]."* You probably mean `[object Object]`. That means you're converting the object to a string (for instance, via `document.write`, or assigning to `innerHTML`, etc.). – T.J. Crowder Nov 14 '17 at 07:33
  • 1
    You are not printing JSON, you are printing an actual object that some browsers show as if being JSON. Try `JSON.stringify(eventUnitResults);` – Randy Nov 14 '17 at 07:34
  • Side note: Your snippet falls prey to [*The Horror of Implicit Globals*](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html): Be sure to declare your variables (such as `i` and `j`). – T.J. Crowder Nov 14 '17 at 07:34
  • Use underscore js for deep picking data. Refer https://stackoverflow.com/questions/39754131/deep-picking-using-underscore-js – VIJAYABAL DHANAPAL Nov 14 '17 at 07:35
  • @Randy - JSON.stringify(eventUnitResults); worked . Thx a ton. – Vijay Nov 14 '17 at 16:44

1 Answers1

2

If you log your objects, you might want to convert the actual object to a string.

Background

If you compare this to java (or most languages):

System.out.println(object);

prints your object.toString(). Unless you override it, that's the memory address.

Problem

In JavaScript:

console.log(object);

[object, object]

would print [object, object] because it prints WHAT you are printing. In this case, it does not know that you expect a String containing JSON.

Note this does not apply to all browsers. Chrome, for example, wants to help you out and prints the JSON value interactively; you can collapse and uncollapse it.

Solution

The solution to this problem, is telling the console explicitly to print a json string. You can do this by calling the build-in json object's function to stringify an object.

JSON.stringify(object);

{ "content": "json" }


For completeness, print the object pretty by setting the print output to 4 spaces indentation:

JSON.stringify(object, null, 4);

prints:

{
    "content": "json"
}
Community
  • 1
  • 1
Randy
  • 9,419
  • 5
  • 39
  • 56
  • +1 & big thanks for providing details about JSON.stringify but Randy gave the right answer to the problem I had. – Vijay Nov 17 '17 at 16:49
  • @Vijay that was me, detailing my comment because then you can close the question by accepting my answer ;) – Randy Nov 18 '17 at 08:30