javascript - How to make a JSON String Valid, if it's not? -


i have requirement json string other api, , string may or may not valid json string. how check if json string valid or not, if it's not valid, how can make valid ? mean ask if there characters need escaped , if not escaped error while parsing. have api make json string valid ?

i got code check if json string valid or not

javascript

if (/^[\],:{}\s]*$/.test('{section : "abcdefghi"jklmnop"}'.replace(/\\["\\\/bfnrtu]/g, '@'). replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[ee][+\-]?\d+)?/g, ']'). replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {   console.log('valid'); }else{     console.log('not valid'); } 

that non trivial problem. depends on definition of "valid" json. can turn string valid json escaping quote characters , treating string. example,

{"foo":"bar 

can become

{"stringified":"{\"foo\":\"bar"} 

and valid, become

{"foo":"bar"} 

or

{"foo":"barcelona"} 

i suggest taking @ domain or providing specific problem set in question.


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -