12

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

4
  • Think you should go with a regular expression, searching for words before the ':'. Commented Sep 6, 2010 at 12:08
  • Why exactly do you want to do this? Commented Sep 6, 2010 at 12:09
  • @GenericTypeTea and @Hogan i am trying to filter my json data clientside with a library like jlinq. It uses the latter type of json string hugoware.net/Projects/jLinq Commented Sep 6, 2010 at 12:12
  • We are suffering the same problem in PHP Symfony. json_encode() quotes json hash keys, which later Google Charts developers.google.com/chart/interactive/docs/… errors out with c[qe] is not a function Commented Nov 12, 2014 at 9:33

2 Answers 2

28

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?

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

5 Comments

What if one of the keys or values has a : in it? Using a JSON parser would be a safer approach.
@patrick, it won't match them, cuz the regular expression specifies double quotes, and a \w+, so it will neither match a "my name is: john" nor "my name is: \"john\": the coder!"
I guess I shouldn't have been so specific with :. Any non \w character will cause the match to fail.
@patrick That's my point too :)! I don't want it to match inside values! and for keys I know it won't match keys with weird names, I said but won't that cuz a problem if the label was a reserved word? in my post. BTW the examples in my previous comment are examples for values, not keys.
Yes, you're right about the values. Not sure why I included that. I see what you mean about the reserved word. Even worse, simply getting rid of the quotes around the keys (like OP requested) would turn a valid JSON string into an invalid one, regardless of reserved words. I think OP was requesting a way to implement a solution that doesn't fix the actual issue.
9

If what you have is actually a JSON string, as in:

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

Then you could parse it with $.parseJSON(), as in:

var result = $.parseJSON( obj );

This will convert your JSON string to javascript objects/arrays.

4 Comments

if possible go through this hugoware.net/Projects/jLinq and see the data.users
@Pandiya - I see it, but what about it should I see?
now see this stackoverflow.com/questions/3636568/… i ve tried the same what you said but it doesn't filter my json data?
@Pandiya - I don't know how you're creating/receiving the data. Is it actually a JSON string? If you're creating a object like: var obj = {"hi","there"};, then the quotes won't hurt anything. But if it is a JSON string like: var obj = '{"hi","there"}';, then using $.parseJSON() will convert it to the first version. You should log your data to the console both ways to see if it is what you expect.

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.