tags:

views:

31

answers:

2

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

+1  A: 

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.

patrick dw
@patrick if possible go through this http://www.hugoware.net/Projects/jLinq and see the data.users
Pandiya Chendur
@Pandiya - I see it, but what about it should I see?
patrick dw
@patrick now see this http://stackoverflow.com/questions/3636568/jlinq-doesnt-seem-to-fetch-all-matching-rows-from-my-json-data i ve tried the same what you said but it doesn't filter my json data?
Pandiya Chendur
@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.
patrick dw
+1  A: 

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, but won't that cuz a problem if the label was a reserved word?

aularon
What if one of the keys or values has a `:` in it? Using a JSON parser would be a safer approach.
patrick dw
@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!"`
aularon
That's my point. It won't match it.
patrick dw
I guess I shouldn't have been so specific with `:`. Any non `\w` character will cause the match to fail.
patrick dw
@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.
aularon
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.
patrick dw