Search code examples
javascriptjqueryjsonstringify

JavaScript DEEP stringify


I'm having a problem getting the right result when "stingifying" a complex JSON object. for example:

{
  "a": "a",
  "b": [
    [
      {
        "c": "c",
        "d": "d",
        "e": "e"
      },
      {
        "c": "c",
        "d": "d",
        "e": "e"
      },
      {
        "c": "c",
        "d": "d",
        "e": "e"
      }
    ],
    [{
        "c": "c",
        "d": "d",
        "e": "e"
      }],
    [
      {
        "c": "c",
        "d": "d",
        "e": "e"
      },
      {
        "c": "c",
        "d": "d",
        "e": "e"
      }
    ]
  ],
  "f": [
    {
      "g": "g",
      "h": "h"
    },
    {
      "g": "g",
      "h": "h"
    },
    {"g": "g"}
  ]
}

In the example above, a and even f stringify great but i'm "loosing" the content of the value of b (stringify as empty array), From what i read if there is more then one level deep in the JSON there is a need to do a deep stringify but i can not seem to get it the right way to see the content of the b key.

Any one have any idea on how to do that?

p.s. the nodes can have even 3 or more levels deep, not just one or two.

Thank you

Update: I've found the problem, I am using $.tmpl and for some reason, in some cases i am getting something like associative array instead of array of objects (I know there is no such thing as associative array in JS but that is what i am getting, array of size 0 with key-value pairs inside it, NOT AN OBJECT) in the value of "b" in the second level. Stringify is doing what I expected it to do now. Thanks


Solution

  • please see: Why doesn't JSON.stringify display object properties that are functions? there is a diagram in the answer showing which values are allowed. "undefined" is not one of them. As also stated in the comments there, undefined values will be omited when using stringify.