0

JSON.stringify() arguments still returns objects in a straight line with slashes // in between.

I added ```JSON.stringify(stringifiedData, null, 4) to my code to prettify my object. But I am still getting all my data in a straight line with slashes in between.

what I am getting:

"{\"stringifiedData\":\"\\\"{\\\\\\\"collection\\\\\\\":[{\\\\\\\"fruit\\\\\\\":\\\\\\\"apple\\\\\\\",\\\\\\\"clothings\\\\\\\":{\\\\\\\"item\\\\\\\":\\\\\\\"winter\\\\\\\"}},{\\\\\\\"fitness\\\\\\\":\\\\\\\"treadmill\\\\\\\",\\\\\\\"mindfulness\\\\\\\":\\\\\\\"app called calm\\\\\\\"}]}\\\"\"}"

Here's the code:

const fs = require("fs").promises;

async function dataReader(filePath, data) {
  const result = await fs.readFile(filePath);
  try {
    return JSON.parse(result);
  } catch (e) {
    console.error(e);
  }
}

//read values.json
(async () => {
  const value = await dataReader("./values.json");

  //read collection.json
  const data = await dataReader("./collections.json");
  
  //replace tokens in `collection.js` with `values.js`
  let stringifiedData = JSON.stringify(data);
  Object.keys(value).forEach((token) => {
    stringifiedData = stringifiedData.replaceAll(`__${token}__`, value[token])
  });
  
  // write/save the new replaced token values in collection.json
  await fs.writeFile("./collections.json", JSON.stringify(stringifiedData, null, 4));
})();

collection.json

{
  "collection" : [
    {
      "fruit": "__fruit_type__",
    "clothings":{
      "item": "__clothing_type__}"
    }
  },
  {
    "fitness": "__fitness_equipment__",
    "mindfulness": "app called __meditation_app__"
  }
]
}
 
**values.json**
{
    "clothing_type": "winter",
   "fruit_type": "apple",
   "fitness_equipment": "treadmill",
   "meditation_app": "calm"
}

expected result:

{
  "collection": [
    {
      "fruit":"apple",
      "clothings":{
        "item":"winter}"
      }
    },
    {
      "fitness":"treadmill",
      "mindfulness":"app called calm"
    }
  ]
}
Tini
  • 169
  • 8
  • 5
    You are calling `JSON.stringify` on a string, what do you expect? Try `JSON.stringify(JSON.parse(stringifiedData), null, 4)` – Konrad Dec 13 '22 at 08:46
  • 2
    @DiegoD the issue is in `JSON.stringify(stringifiedData)` where `stringifiedData` is already a JSON string – adiga Dec 13 '22 at 08:51
  • @Konrad Yeah, figured that out. Thank you. – Tini Dec 13 '22 at 09:06
  • @Konrad, I am having some syntax trouble. my function only works once and I am trying to get it to run whenever I update my values by making this change: ```await fs.writeFile("./collections.json", JSON.stringify(JSON.parse({data:stringifiedData}), null, 4));```. This fails with the error ```SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse ()``` Please what's the correct syntax? – Tini Dec 13 '22 at 09:55
  • 1
    `JSON.stringify({data: JSON.parse(stringifiedData)}, null, 4)` – Konrad Dec 13 '22 at 09:57
  • @Konrad, thanks. Not getting the syntax error again, but what this does is just to append `data` to my `collections.json` – Tini Dec 13 '22 at 14:44

0 Answers0