DEV Community

Discussion on: Pretty JSON Output

Collapse
 
qm3ster profile image
Mihail Malo • Edited

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":"🍤",'))