I have been trying to convert this piece of string to proper JSON obj, after many string manipulation and trials and even after using JSON.parse and stringify funcs also couldnt help me. Can someone help me coverting this string to Proper JSON Obj?
"{\n status: 'success',\n message: 'The user is able to enter data in search bar.'\n}\n{ status: 'success', message: 'Number of cities with letter p are 4.' }\n{\n status: 'success',\n message: 'Success messagecity addedappears in green'\n}\n{\n status: 'success',\n message: 'The city added is visible under Cities header.'\n}\n"
I tried using JSON.parse and stringify functions, also replaceAll,split(),but not helped.
CodePudding user response:
const messageArray = str.replaceAll("\n","").replaceAll("}{","},{").replaceAll('status:', '"status":').replaceAll('message:', '"message":').replaceAll("'", '"').match(/{(.)*?}/g).map(i => JSON.parse(i))
But it's a terrible way. It is better to bring to a normal view on the part of the sender of this disgrace.
CodePudding user response:
- The string have some missing comas in each object, so the first thing is to add those commas.
- The string is a bunch of separated objects, so lets mix them together into an array before convert it to json.
const jsObjecStringWithoutN = document.querySelector('#jsObjecStringWithoutN');
const jsObjecStringAddingCommas = document.querySelector('#jsObjecStringAddingCommas');
const JSONObject = document.querySelector('#JSONObject');
const javascriptObject = "{\n status: 'success',\n message: 'The user is able to enter data in search bar.'\n}\n{ status: 'success', message: 'Number of cities with letter p are 4.' }\n{\n status: 'success',\n message: 'Success messagecity addedappears in green'\n}\n{\n status: 'success',\n message: 'The city added is visible under Cities header.'\n}\n";
// replace the \\n character
const replaceEnter = javascriptObject.replaceAll('\n', '');
jsObjecStringWithoutN.innerText = replaceEnter;
// add commas at the end of each separated object
const addingCommas = replaceEnter.replaceAll('}{', '},{');
jsObjecStringAddingCommas.innerText = addingCommas;
//replace single quotes to double quotes
const replacingQuotes = addingCommas.replaceAll("\'",'"');
let jsonString = "[" replacingQuotes "]";
// put double quotes in keys
const jsonObjectText = jsonString.replaceAll(/[a-zA-Z0-9] :/ig, (key)=>{
const formattedKey = key.replace(':','');
return '"' formattedKey '":';
});
const jsonObject = JSON.parse(jsonObjectText);
JSONObject.innerText = JSON.stringify(jsonObject);
code{
background: #ececec;
}
<h2>Javascript Object</h1>
<code id="jsObjectString">{\n status: 'success',\n message: 'The user is able to enter data in search bar.'\n}\n{ status: 'success', message: 'Number of cities with letter p are 4.' }\n{\n status: 'success',\n message: 'Success messagecity addedappears in green'\n}\n{\n status: 'success',\n message: 'The city added is visible under Cities header.'\n}\n"</code>
<h3>First step, delete "/n"</h3>
<code id="jsObjecStringWithoutN"></code>
<h3>Second step, adding commas</h3>
<code id="jsObjecStringAddingCommas"></code>
<h3>Third step, wrap it all up in one array of objects and convert it to JSON</h3>
<code id="JSONObject"></code>