JSFiddle - React, Tailwind, and code Playground
JavaScript
function stringify(obj_from_json) {
if (typeof obj_from_json !== "object" || Array.isArray(obj_from_json)) {
// not an object, stringify using native function
return JSON.stringify(obj_from_json);
}
// Implements recursive object serialization according to JSON spec
// but without quotes around the keys.
let props = Object.keys(obj_from_json)
.map((key) => `${key}:${stringify(obj_from_json[key])}`)
.join(",");
return `{${props}}`;
}
var obj = {
name: "John Smith",
favoriteObjects: [
{ b: "there's", c: "always", d: "something" },
[1, 2, "spam", { f: "hello" }],
{ vowels: "are missing" },
],
job: {
name: "Accountant",
salary: 1000,
},
favoriteFruits: ["Apple", "Banana"],
};
document.body.innerHTML = `<pre>${stringify(obj)}</pre>`;