0

I am trying to figure out what the "null" means?

JSON.stringify(myJsonObj, null, 2)

I see people using "undefined" or "null"

Couldn't figure out what the "null" was for so I just removed it.

JSON.stringify(myJsonObj)

So now I start getting these

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)

Could this be the reason?

What exactly does Null do?

torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53
  • 4
    [Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters) – Tushar Nov 16 '17 at 05:31
  • Possible duplicate of [JSON.stringify, avoid TypeError: Converting circular structure to JSON](https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json) – Derek Brown Nov 16 '17 at 06:04
  • @DerekBrown my question goes more on what the "replacer" (null) does? – torbenrudgaard Nov 16 '17 at 06:20
  • Using `null` as the replacer function is the same as not providing the argument at all -- the only reason to do that is so you can provide the `space` argument without a replacer. It shouldn't affect whether you get an error. – Barmar Nov 16 '17 at 07:05

1 Answers1

2

JSON.stringify; can take 3 parameters, (value,replacer,space)

  • value is the object you want to stringify, in your case myJsonObj
  • replacer A function that alters the behavior of the stringification process.
  • space object that's used to insert white space into the output JSON string for readability purposes

in your case the myJsonObj contains circular structure which needs to be corrected in the replacer function maybe you can get help from here: JSON.stringify, avoid TypeError: Converting circular structure to JSON to know how you can convert you objects within "myJsonObj".

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • @Derek "A function that alters the behavior of the stringification process" - how is it altered? Does it skip fields with null value or? There is very little info about this online. – torbenrudgaard Nov 16 '17 at 06:19
  • @torbenrudgaard I did not write this answer- am just the editor. There is a lot about this online. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter – Derek Brown Nov 16 '17 at 06:20