0

I have string which is in form of JSON but not a valid JSON string. String is like as below (Its single line string but I have added new lines for clarity.)

"{
   clientId :\"abc\",
   note:\"ATTN:Please take care of item x\"
}"

I am trying to fix it (reformating to valid JSON) using javascript regular expression. I am currently using following regular expression but its not working for second property i.e. note as it has colon (:) in its value.

retObject.replace(/(['"])?([a-zA-Z0-9_]+)(['"])?:/g, '"$2": ');

What I am trying to do here is using regular expression to reformat above string to

"{
   "clientId" :"abc",
   "note":"ATTN:Please take care of item x"
 }"

Tried many ways but couldnt get it just right as I am still beginer in RegEx.

4
  • 1
    It’s probably invalid because that “string” has multiple lines which doesn’t work like that in JavaScript. Commented Oct 19, 2015 at 16:29
  • There are many similar questions on SO which use RegEx..please look at them Commented Oct 19, 2015 at 16:32
  • Its single line string. I have added new lines for clarity. Sorry about that. I should have mentioned it in post. Commented Oct 19, 2015 at 16:33
  • If these answers helped you, accept one of them. Commented Oct 24, 2015 at 13:55

2 Answers 2

0

Try using .split() with RegExp /[^\w\s\:]/ , .test() with RegExp /\:$/ , .match() with RegExp /\w+/

var str = "{clientId :\"abc\",note:\"ATTN:Please take care of item x\"}";
var res = {};
var arr = str.split(/[^\w\s\:]/).filter(Boolean);
for (var i = 0; i < arr.length; i++) {
  if ( /\:$/.test(arr[i]) ) {
    res[ arr[i].match(/\w+/) ] = arr[i + 1]
  }
}

console.log(res)

Sign up to request clarification or add additional context in comments.

2 Comments

I'm very confused by what a character set such as [^\w+\s+|\:] is donig with those + and | inside it. What you wrote means, "match any character other than a letter, plus sign, alphanumeric, vertical bar, or colon". Is that what you want?
@torazaburo "Is that what you want?" No ; attempting to match { , } , ,; see updated post . Though , if these characters within string , would require further adjustments . Thanks for the character set rule reminder
0

Trying to fix broken JSON with a regexp is a fool's errand. Just when you think you have the regexp working, you will be presented with additional gobbledygook such as

"{ clientId :\"abc\", note:\"ATTN:Please take \"care\" of item x\" }"

where one of the strings has double quotes inside of it, and now your regexp will fail.

For your own sanity and that of your entire team, both present and future, have the upstream component that is producing this broken JSON fixed. All languages in the world have perfectly competent JSON serializers which will create conformant JSON. Tell the upstream folks to use them.

If you have absolutely no choice, use the much-reviled eval. Meet evil with evil:

eval('(' + json.replace(/\\"/g, '"') + ')')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.