re: Pretty JSON Output VIEW POST

FULL DISCUSSION
 

Doesn't seem like the output of JSON.stringify(protein, null, "I 💛") is recoverable with JSON.parse.

 

oh ya 😮 ...looking into the docs. it seems like:
Throws a SyntaxError exception if the string to parse is not valid JSON.

Definitely something to keep in mind before using the "space" parameter. thanks for catching that!

 

I was just curious if there would be a way to recover, for example another parameter you could pass to parse. There isn't though.

const protein = {
  steak: "🥩",
  bacon: "🥓",
  fake: { shrimp: "🍤" },
  array: ["🥩", "🥓", "🍤"]
}
// looks weird but works
JSON.parse(JSON.stringify(protein, null, "\n\t\n\0\r  "))
// SyntaxError
JSON.parse(JSON.stringify(protein, null, "🥓"))

// and my favorite:
// parses as `{sh: "🍤", steak: "🥩", bacon: "🥓"}`
// but breaks nested objects or arrays.
JSON.parse(JSON.stringify({ steak: "🥩", bacon: "🥓" }, null, '"sh":"🍤",'))
code of conduct - report abuse