Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove quotes from keys in a json string using jquery

Tags:

json

jquery

key

Consider this as my json string,

{"Table" : [{"userid" : "11","name" : "KumarP","designation" : "Business Head",
"phone" : "9789234793","email" : "[email protected]","role" : "Admin",
   "empId" : "EI003","reportingto" : "KumarP"}]}

and i want to have my string like this,

{Table:[{ userid: "11", name: "KumarP", designation: "Business Head", 
    phone: "9789234793", email:"[email protected]", role : "Admin",
       empId : "EI003",reportingto : "KumarP"}]}

I am doing so to use it with jlinq..

like image 346
ACP Avatar asked Sep 06 '10 12:09

ACP


People also ask

How do I remove quotes from JSON Stringify?

That makes the regex to remove the quotes from the keys MUCH easier. Start your solution with this: var cleaned = JSON. stringify(x, null, 2);

How do you remove quotes from JQ output?

If you want to strip the quotes, just pipe the output from this command to tr -d '"' .

How do I remove all quotes from a string?

Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.

How do you remove double quotes from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.


1 Answers

Use Regular Expressions:

var a='{"Table" : [{"userid" : "11","name" : "KumarP","designation" : "Business Head","phone" : "9789234793","email" : "[email protected]","role" : "Admin",    "empId" : "EI003","reportingto" : "KumarP"}]}';
a=a.replace(/"(\w+)"\s*:/g, '$1:');
alert(a);

The string will become as your second codeblock:

{Table: [{userid: "11",name: "KumarP",designation: "Business Head",phone: "9789234793",email: "[email protected]",role: "Admin",    empId: "EI003",reportingto: "KumarP"}]}

But won't that cause a problem if the label was a reserved word?

like image 131
aularon Avatar answered Sep 18 '22 00:09

aularon