Home > Back-end >  Turning a string into a valid JSON
Turning a string into a valid JSON

Time:08-31

So currently I have a JSON that looks something like this: const str = `{'Hi': 'hello I'm good'}`

So what I am trying to do is to turn the: ' into " except those in a string already, how could I achieve this?

CodePudding user response:

This replaces the character with a double quote and makes it valid JSON.

const str = `{'Hi': 'hello I'm good'}`;

let replace = str.replace('`', '"');
console.log(replace);

CodePudding user response:

var key = {a:"b",c:"d"};

var l = JSON.stringify(key);

localStorage.setItem('test',l);

var local_storage = $.parseJSON(localStorage.getItem('test'));

console.log(local_storage);

console.log(local_storage.a);
  • Related