0

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.

  • First, [a JavaScript object is not JSON](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation). To parse this properly you want the string that you are trying to parse to be valid JSON. So your best option is to change this at the source of where this string is coming from. Then you can simply use `JSON.parse()` on it. Currently the string appears to be in some format that is easy to read for humans, not machines. (It _looks_ like a valid JavaScript object literal (not JSON), but it is also missing comma's between the objects to be valid.) – Ivar Dec 29 '22 at 12:03
  • JSON is very strict, one idea is to use a more relaxed parser, https://www.npmjs.com/package/really-relaxed-json – Keith Dec 29 '22 at 12:11
  • If the text is human generated, then fix it manually. If it's machine generated, then have the machine export proper JSON (every language has libs or built-in functions for JSON). Resolve the root problem rather than trying to find a workaround. – msenne Dec 29 '22 at 13:58

2 Answers2

0
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.

0
  1. The string have some missing comas in each object, so the first thing is to add those commas.
  2. 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>